Python string methods are handy tools for data manipulation and formatting. They serve as the foundation of text processing in Python. In this article, we're going to cover four crucial Python string methods - upper()
, capitalize()
, title()
, and lower()
. You'll learn how to use these powerful tools in your Python scripts.
The upper()
method returns a copy of the original string and converts all the characters into uppercase.
string = "Hello, Python!"
print(string.upper())
Output:
HELLO, PYTHON!
The capitalize()
method makes the first letter of the string a capital (uppercase) letter, while making all other letters in the string lowercase letters.
string = "hello, Python!"
print(string.capitalize())
Output:
Hello, python!
The title()
method capitalizes the first letter of each word in the string and makes all other letters lowercase.
string = "hello, Python!"
print(string.title())
Output:
Hello, Python!
The lower()
method converts all uppercase characters in a string into lowercase characters and returns it.
string = "Hello, Python!"
print(string.lower())
Output:
hello, python!
Mastering these string methods will open a new horizon in your Python journey. They are simple, yet very powerful tools that could make your data manipulation tasks in Python much easier and efficient. Remember, the best way to learn is by doing. So, start writing your Python scripts and use these methods as much as you can.
# Full code
string = "Hello, Python!"
print("Upper:", string.upper())
print("Capitalize:", string.capitalize())
print("Title:", string.title())
print("Lower:", string.lower())
FAQs
capitalize()
and title()
methods?
The capitalize()
method only makes the first character of the string uppercase, while title()
method makes the first character of each word in the string uppercase.CloneCoding
Innovation Starts with a Single Line of Code!