String and Number Manipulation
π§΅ String methods
len(): Returns the length of the string.find(): Returns the index of the first occurrence of a substring.rfind(): Returns the index of the last occurrence of a substring.count(): Returns the number of occurrences of a substring in the string.replace(): Replaces a substring with another substring.capitalize(): Capitalizes the first character of the string.title(): Capitalizes the first character of each word in the string.upper(): Converts the string to uppercase.lower(): Converts the string to lowercase.split(): Splits the string into a list of substrings.join(): Joins a list of strings into a single string.strip(): Removes leading and trailing whitespace.startswith(): Checks if the string starts with a specified substring.endswith(): Checks if the string ends with a specified substring.isalpha(): Checks if all characters in the string are alphabetic.isdigit(): Checks if all characters in the string are digits.
string_methods.py
text = "Hello, World!"
print("Length:", len(text)) # Output: 13
print("Index of 'World':", text.find("World")) # Output: 7
print("Last index of 'o':", text.rfind("o")) # Output: 8
print("Count of 'o':", text.count("o")) # Output: 2
print("Replaced 'World':", text.replace("World", "Python")) # Output: Hello, Python!
print("Capitalized:", text.capitalize()) # Output: Hello, world!
print("Title:", text.title()) # Output: Hello, World!
print("Uppercase:", text.upper()) # Output: HELLO, WORLD!
print("Lowercase:", text.lower()) # Output: hello, world!
print("Split:", text.split(", ")) # Output: ['Hello', 'World!']
print("Join:", ", ".join(['Hello', 'World!'])) # Output: Hello, World!
print("Strip:", text.strip("!")) # Output: Hello, World
print("Starts with 'Hello':", text.startswith("Hello")) # Output: True
print("Ends with 'World!':", text.endswith("World!")) # Output: True
print("Is alpha:", text.isalpha()) # Output: False
print("Is digit:", text.isdigit()) # Output: Falseπ string indexing
In Python, strings are indexed, meaning each character in a string has a unique index starting from 0. You can access individual characters or slices of the string using these indices.
string_indexing.py
text = "Hello, World!"
print("First character:", text[0]) # Output: H
print("Last character:", text[-1]) # Output: !
print("Substring (0-4):", text[0:5]) # Output: Hello
print("Substring (7-12):", text[7:12]) # Output: World
print("Substring (7 to end):", text[7:]) # Output: World!
print("Substring (start to 5):", text[:5]) # Output: Hello
print("Every second character:", text[::2]) # Output: Hlo ol!
print("Reversed string:", text[::-1]) # Output: !dlroW ,olleH𦴠Format specifiers for numbers
Format specifiers are used to format strings in Python. They allow you to control how values are displayed in a string.
format_specifiers.py
# Format specifiers for numbers
number = 123.456789
print("Default:", number) # Output: 123.456789
print(f"Two decimal places: {number:.2f}") # Output: 123.46
print(f"Three decimal places: {number:.3f}") # Output: 123.457
print(f"Scientific notation: {number:.2e}") # Output: 1.23e+02
print(f"Percentage: {number:.2%}") # Output: 12345.68%
# Padding and alignment
print(f"Left aligned: {'Hello':<10}") # Output: Hello
print(f"Right aligned: {'World':>10}") # Output: World
print(f"Center aligned: {'Python':^10}") # Output: Python
print(f"Zero-padded: {42:05d}") # Output: 00042
print(f"Hexadecimal: {255:x}") # Output: ff
print(f"Binary: {10:b}") # Output: 1010
print(f"Octal: {8:o}") # Output: 10
print(f"Formatted with commas: {1000000:,}") # Output: 1,000,000
print(f"Formatted with thousands separator: {1234567.89:,.2f}") # Output: 1,234,567.89Last updated on