Python string : methods
capitalize()
: Converts the first character to uppercase.casefold()
: Similar to lowercasing, but more aggressive for comparing strings.center(width[, fillchar])
: Returns a centered string.count(sub[, start[, end]])
: Returns the number of occurrences of a substring.encode([encoding[, errors]])
: Returns an encoded version of the string.endswith(suffix[, start[, end]])
: Returns True if the string ends with the specified suffix.find(sub[, start[, end]])
: Returns the lowest index of the substring.format(*args, **kwargs)
: Formats the string.isalnum()
: Returns True if all characters in the string are alphanumeric.join(iterable)
: Concatenates elements of an iterable to the string.lower()
: Converts all characters to lowercase.upper()
: Converts all characters to uppercase.replace(old, new[, count])
: Replaces occurrences of a substring with another string.split([sep[, maxsplit]])
: Splits the string at the specified separator.strip([chars])
: Removes leading and trailing characters (default is whitespace).startswith(prefix[, start[, end]])
: Returns True if the string starts with the specified prefix.title()
: Converts the first character of each word to uppercase.isalpha()
: Returns True if all characters in the string are alphabetic- isascii() returns True if all of the characters in the string are ascii characters.
- isdecimal() Returns True if all of the characters in the string are decimals.
- isdigit() Returns True if all of the characters in the string are digits.
- isidentifier() Returns If the string is an identifier, it is true.
- islower() Returns True if all of the characters in the string are lower case.
- isnumeric() Returns True if all of the characters in the string are numbers.
- isprintable() returns true True if all of the characters in the string are printable.
- isspace() Returns True if all of the characters in the string are whitespaces.
- istitle() returns True if the string adheres to the rules of a title.
- isupper() Returns True if all of the characters in the string are upper case.
- join() converts the elements of an iterable to a string.
- ljust() returns a left justified version of the string.
- lower() converts a string to lower case.
- lstrip() returns a left-trimmed version of the string.
- maketrans() returns a translation table for use in translations.
- partition() returns a tuple containing three parts of a string.
- replace() returns a string in which a specified value is replaced with another specified value.
- rfind() searches the string for a given value and returns the last position it was found.
- rindex() searches the string for a specified value and returns the last position where it was found.
- rjust() returns a right-justified version of the string.
- rpartition() returns a tuple containing the string divided into three parts.
- rsplit() splits the string at the separator specified and returns a list.
- rstrip() returns a string that has been correctly trimmed.
- split() divides a string at the specified separator and returns a list of the results.
- splitlines() returns a list after splitting the string at line breaks.
- returns true if the string begins with the specified value.
- strip() returns a trimmed version of the string.
- swapcase() swaps cases, making lower case upper case and vice versa.
- title() changes the first character of each word to upper case.
- translate() produces a string that has been translated.
- upper() converts a string to upper case.
- zfill() Fills the string with a specified number of 0 values at the beginning
Python string methods are essential for efficiently manipulating and working with textual data. They provide a variety of functions, including:
- String Modification:
- Methods like
upper()
,lower()
,capitalize()
, andtitle()
enable you to change the case of characters in a string, facilitating consistent formatting.
- Search and Replace:
find()
,index()
, andreplace()
help locate substrings and replace them, aiding in text manipulation and modification.
- String Formatting:
- Methods like
format()
and f-strings simplify string formatting, making it easier to create dynamic and readable output.
- Whitespace Handling:
strip()
,lstrip()
, andrstrip()
allow you to remove leading and trailing whitespaces, enhancing data cleanliness and presentation.
- String Splitting and Joining:
split()
andjoin()
assist in breaking strings into lists or combining lists into strings, aiding in data parsing and aggregation.
- Checking and Validation:
- Methods such as
startswith()
,endswith()
, andisalnum()
help validate and verify strings, ensuring they meet specific criteria.
- Character Count and Manipulation:
count()
andreplace()
enable you to count occurrences of characters or replace specific characters within a string.
- Substring Extraction:
- Methods like
slice
notation andsplit()
assist in extracting specific portions of a string, providing flexibility in data extraction.
- String Comparison:
- Techniques like lexicographical comparison and methods like
casefold()
help in comparing strings, crucial for sorting and organizing data.
- Regular Expressions:
- Python’s
re
module works hand-in-hand with string methods, allowing powerful pattern matching and manipulation for complex text processing tasks.
- Python’s
In conclusion, Python string methods are essential for handling and manipulating textual data, providing a rich set of tools to streamline various text-related operations in a convenient and efficient manner.