Python has a set of built-in methods that you can use on strings.
Note: All string methods returns new values. They do not change the original string.
Example
Upper case the first letter in this sentence:
txt = “hello, and welcome to my world.”
x = txt.capitalize()
print (x)
Output:
Hello, and welcome to my world.
| Method | Syntax | Description |
|---|---|---|
| capitalize() | string.capitalize() | Converts the first character to upper case |
| casefold() | string.casefold() | Converts string into lower case |
| center() | string.center(length, character) | Returns a centered string |
| count() | string.count(value, start, end) | Returns the number of times a specified value occurs in a string |
| encode() | string.encode(encoding=encoding, errors=errors) | Returns an encoded version of the string |
| endswith() | string.endswith(value, start, end) | Returns true if the string ends with the specified value |
| expandtabs() | string.expandtabs(tabsize) | Sets the tab size of the string |
| find() | string.find(value, start, end) | Searches the string for a specified value and returns the position of where it was found |
| format() | string.format(value1, value2…) | Formats specified values in a string |
| format_map() | Formats specified values in a string | |
| index() | string.index(value, start, end) | Searches the string for a specified value and returns the position of where it was found |
| isalnum() | string.isalnum() | Returns True if all characters in the string are alphanumeric |
| isalpha() | string.isalpha() | Returns True if all characters in the string are in the alphabet |
| isdecimal() | string.isascii() | Returns True if all characters in the string are decimals |
| isdigit() | string.isdecimal() | Returns True if all characters in the string are digits |
| isidentifier() | string.isdigit() | Returns True if the string is an identifier |
| islower() | string.isidentifier() | Returns True if all characters in the string are lower case |
| isnumeric() | string.isprintable() | Returns True if all characters in the string are numeric |
| isprintable() | string.isprintable() | Returns True if all characters in the string are printable |
| isspace() | string.isspace() | Returns True if all characters in the string are whitespaces |
| istitle() | string.istitle() | Returns True if the string follows the rules of a title |
| isupper() | string.isupper() | Returns True if all characters in the string are upper case |
| join() | string.join(iterable) | Joins the elements of an iterable to the end of the string |
| ljust() | string.ljust(length, character) | Returns a left justified version of the string |
| lower() | string.lower() | Converts a string into lower case |
| lstrip() | string.lstrip(characters) | Returns a left trim version of the string |
| maketrans() | string.maketrans(x, y, z) | Returns a translation table to be used in translations |
| partition() | string.partition(value) | Returns a tuple where the string is parted into three parts |
| replace() | string.replace(oldvalue, newvalue, count) | Returns a string where a specified value is replaced with a specified value |
| rfind() | string.rfind(value, start, end) | Searches the string for a specified value and returns the last position of where it was found |
| rindex() | string.rindex(value, start, end) | Searches the string for a specified value and returns the last position of where it was found |
| rjust() | string.rjust(length, character) | Returns a right justified version of the string |
| rpartition() | string.rpartition(value) | Returns a tuple where the string is parted into three parts |
| rsplit() | string.rsplit(separator, maxsplit) | Splits the string at the specified separator, and returns a list |
| rstrip() | string.rstrip(characters) | Returns a right trim version of the string |
| split() | string.split(separator, maxsplit) | Splits the string at the specified separator, and returns a list |
| splitlines() | string.splitlines(keeplinebreaks) | Splits the string at line breaks and returns a list |
| startswith() | string.startswith(value, start, end) | Returns true if the string starts with the specified value |
| strip() | string.strip(characters) | Returns a trimmed version of the string |
| swapcase() | string.swapcase() | Swaps cases, lower case becomes upper case and vice versa |
| title() | string.title() | Converts the first character of each word to upper case |
| translate() | string.translate(table) | Returns a translated string |
| upper() | string.upper() | Converts a string into upper case |
| zfill() | string.zfill(len) | Fills the string with a specified number of 0 values at the beginning |
