- In Python, a string is a sequence of characters, enclosed in single quotes (‘…’) or double quotes (“…”).
- Strings are used for representing text values and are one of the most basic data types in Python.
Below is a table of common string methods:
Method Name | Description | Syntax | Example |
---|---|---|---|
str.upper() | Converts all characters in the string to uppercase | str.upper() | "car".upper() returns "CAR" |
str.lower() | Converts all characters in the string to lowercase | str.lower() | "CAR".lower() returns "car" |
str.capitalize() | Converts the first character of the string to uppercase and all other characters to lowercase | str.capitalize() | "car".capitalize() returns "Car" |
str.title() | Converts the first letter of each word in the string to uppercase | str.title() | "my first car".title() returns "My First Car" |
str.split(sep) | Splits the string into a list of strings using a specified separator sep | str.split(sep) | "car, bike, truck".split(", ") returns ["car", "bike", "truck"] |
str.join(iterable) | Joins a list of strings with the string as a separator | sep.join(iterable) | ", ".join(["car", "bike", "truck"]) returns "car, bike, truck" |
str.replace(old, new) | Replaces all occurrences of old with new in the string | str.replace(old, new) | "car".replace("c", "b") returns "bar" |
str.count(sub) | Returns the number of occurrences of sub in the string | str.count(sub) | "car".count("a") returns 1 |
str.startswith(prefix) | Returns True if the string starts with prefix , False otherwise | str.startswith(prefix) | "car".startswith("c") returns True |
str.endswith(suffix) | Returns True if the string ends with suffix , False otherwise | str.endswith(suffix) | "car".endswith("r") returns True |
More String methods:
Method Name | Description | Syntax | Example |
---|---|---|---|
str.strip() | Removes whitespace characters from the beginning and end of the string | str.strip() | " car ".strip() returns "car" |
str.lstrip() | Removes whitespace characters from the beginning of the string | str.lstrip() | " car".lstrip() returns "car" |
str.rstrip() | Removes whitespace characters from the end of the string | str.rstrip() | "car ".rstrip() returns "car" |
str.find(sub) | Returns the lowest index in the string where sub is found, or -1 if not found | str.find(sub) | "car".find("a") returns 1 |
str.index(sub) | Returns the lowest index in the string where sub is found, or raises a ValueError if not found | str.index(sub) | "car".index("a") returns 1 |
str.isdigit() | Returns True if all characters in the string are digits, False otherwise | str.isdigit() | "123".isdigit() returns True |
str.isalpha() | Returns True if all characters in the string are alphabetic, False otherwise | str.isalpha() | "abc".isalpha() returns True |
str.isalnum() | Returns True if all characters in the string are alphanumeric, False otherwise | str.isalnum() | "abc123".isalnum() returns True |
Python code example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
word = "car" # `str.upper()` method print("Uppercase:", word.upper()) # `str.lower()` method print("Lowercase:", word.lower()) # `str.strip()` method print("Stripped:", " car ".strip()) # `str.lstrip()` method print("Left-stripped:", " car".lstrip()) # `str.rstrip()` method print("Right-stripped:", "car ".rstrip()) # `str.find(sub)` method print("'a' found at index:", word.find("a")) # `str.index(sub)` method print("'a' found at index:", word.index("a")) # `str.isdigit()` method print("Is digit:", word.isdigit()) # `str.isalpha()` method print("Is alphabetic:", word.isalpha()) # `str.isalnum()` method print("Is alphanumeric:", word.isalnum()) |
Output:
Uppercase: CAR
Lowercase: car
Stripped: car
Left-stripped: car
Right-stripped: car
‘a’ found at index: 1
‘a’ found at index: 1
Is digit: False
Is alphabetic: True
Is alphanumeric: True