String Methods
String (str) methods allow you to manipulate and analyze text efficiently. They are essential in tasks such as file processing, user input, report generation, and in general, any system that works with textual data. Thanks to their simplicity and expressiveness, these methods are a key tool in cleaning, transforming, and searching for textual information.
Function | Description |
---|---|
.lower() | Converts all characters to lowercase. |
.upper() | Converts all characters to uppercase. |
.capitalize() | Converts the first character to uppercase and the rest to lowercase. |
.title() | Converts the first letter of each word to uppercase. |
.strip() | Removes whitespace at the beginning and end of a string. |
.replace() | Replaces one substring with another. |
.split() | Divides the string into a list according to a separator. |
.join() | Joins elements of a sequence using the string as a separator. |
.find() | Returns the position of the first occurrence of a substring, or -1 if it does not exist. |
.startswith() | Returns True if the string starts with a given substring. |
.endswith() | Returns True if the string ends with a given substring. |
Below I will explain in more detail how these functions work:
.lower()
Section titled “.lower()”Typical use: Convert a string to lowercase.
text = 'Hello World'print(text.lower())
Output:
hello world
.upper()
Section titled “.upper()”Typical use: Convert a string to uppercase.
text = 'Hello World'print(text.upper())
Output:
HELLO WORLD
.capitalize()
Section titled “.capitalize()”Typical use: Capitalize only the first letter and lowercase the rest.
text = 'hello WORLD'print(text.capitalize())
Output:
Hello world
.title()
Section titled “.title()”Typical use: Convert the first letter of each word to uppercase.
text = 'hello world from PyDocs'print(text.title())
Output:
Hello World From Pydocs
.strip()
Section titled “.strip()”Typical use: Remove whitespace at the beginning and end.
text = ' hello world 'print(text.strip())
Output:
hello world
.replace()
Section titled “.replace()”Typical use: Replace one substring with another.
text = 'I like Python'print(text.replace('Python', 'JavaScript'))
Output:
I like JavaScript
.split()
Section titled “.split()”Typical use: Split a string into a list.
text = 'red,green,blue'print(text.split(','))
Output:
['red', 'green', 'blue']
.join()
Section titled “.join()”Typical use: Join elements of a list with a separator.
colors = ['red', 'green', 'blue']print(', '.join(colors))
Output:
red, green, blue
.find()
Section titled “.find()”Typical use: Find the position of a substring.
text = 'search in this phrase'print(text.find('this'))
Output:
10
.startswith()
Section titled “.startswith()”Typical use: Verify if the string starts with a substring.
file = 'image.png'print(file.startswith('image'))
Output:
True
.endswith()
Section titled “.endswith()”Typical use: Verify if the string ends with a substring.
file = 'image.png'print(file.endswith('.png'))
Output:
True