Fun with Python String Methods
python
Hello, fellow Python enthusiast! 🚀 In this tutorial, we’ll explore the fascinating world of Python string methods—a must-know for every aspiring programmer.
Working with Python, one of the most dynamic and enjoyable programming languages, often involves manipulating strings. Unlike list methods, string methods return a new string value rather than modifying the original string. Why? Because strings are immutable! Let’s explore some captivating string methods together.
Immutable Strings
Imagine you have a string:
my_string = "Hello, world!"
Trying to change a character directly will result in an error since strings are immutable. However, Python offers various string methods to transform and play with your text.
Uppercase and Lowercase Fun
Want to convert your text to uppercase or lowercase? It’s easy!
my_string = "Hello, world!"
print(my_string.upper()) # HELLO, WORLD!
The original string remains unchanged. To keep the modified version, assign it to a new variable:
my_new_string = my_string.upper()
print(my_new_string) # HELLO, WORLD!
Capitalizing Adventures
Capitalize the first letter of a string or every word in a sentence:
txt = "sky majour"
new_txt = txt.capitalize()
print(new_txt) # Sky majour
greeting = "hello world!"
new_greeting = greeting.title()
print(new_greeting) # Hello World!
Searching for Characters
Find the position of a character in a string with find()
:
city = "Berlin"
find_letter_e = city.find("e")
print(find_letter_e) # 1
If the character isn’t found, find()
returns -1
.
String Length and Character Checks
Find the length of your text or check if it’s all lowercase:
txt = "Hello, world! I couldn't believe learning Python could be so much fun."
txt_count = len(txt)
print(txt_count) # 70
txt = "cat"
is_txt_lower = txt.islower()
print(is_txt_lower) # True
String Content Checks
Check if your string contains only alphabetic characters or alphanumeric characters:
txt = "MyPetIsAwesome"
new_txt = txt.isalpha()
print(new_txt) # True
txt = "TextLength12"
new_txt = txt.isalnum()
print(new_txt) # True
A space in the string will make these methods return False
.
String Joinery and Splitting
Join a list of strings or split a sentence into words:
name = ", "
name_string = name.join(["sugar", "potato", "oil", "chicken"])
print(name_string) # sugar, potato, oil, chicken
my_string = "learning python is fun"
list_string = my_string.split()
print(list_string) # ['learning', 'python', 'is', 'fun']
Justifying Text and Stripping Spaces
Align your text left, right, or centre with ljust()
, rjust()
, and center()
:
txt = "Hello"
new_txt = txt.rjust(10)
print(new_txt)
Output:
Hello
txt = "Hello"
new_txt = txt.ljust(10)
print(new_txt)
Output:
Hello
txt = "Hello"
new_txt = txt.center(10)
print(new_txt)
Output:
Hello
Strip unwanted spaces:
txt = " Mango "
new_txt = txt.strip()
print("Hello, world!", new_txt, "tastes good!") # Hello, world! Mango tastes good!
Replacing and Counting
Replace words in your text:
txt = """John Piper picked a peck of pickled peppers,
A peck of pickled peppers John Piper picked."""
new_txt = txt.replace("John", "Peter")
print(new_txt)
Output:
Peter Piper picked a peck of pickled peppers,
A peck of pickled peppers Peter Piper picked
Specify the number of replacements:
txt = """John Piper picked a peck of pickled peppers,
A peck of pickled peppers John Piper picked."""
new_txt = txt.replace("John", "Peter", 1)
print(new_txt)
# Replace only the first "John" with "Peter"
Output:
Peter Piper picked a peck of pickled peppers,
A peck of pickled peppers John Piper picked
And there you have it! Your string manipulation adventure has just begun. Keep exploring, and happy coding!🚀💻