In this article, discover essential Python string methods for manipulating text. Learn how to utilize strip() to remove leading and trailing whitespace, swapcase() to toggle letter cases, title() to capitalize words, translate() to replace characters, upper() to convert text to uppercase, and zfill() to pad numbers with zeros. Enhance your Python string handling skills with these powerful functions.
Before moving ahead, let’s take a look at Python String Methods.
Table of Contents
strip() method
strip() removes the unnecessary characters from beginning and end of the string.
Syntax – string.strip(characters)
Parameter Values –
characters – Optional. A set of characters to be removed.
Example: Returns string after removing unnecessary characters.
text = " I'm reading articles on codingstreets. "
Strip = text.strip()
print(Strip)
#output:
I'm reading articles on codingstreets.
Example: Returns string after removing unnecessary characters.
text = "I'm reading articles on codingstreets..>.."
Strip = text.strip(".>")
print(Strip)
#output:
I'm reading articles on codingstreets
swapcase() method
swapcase() returns the string after changing the uppercase to lowercase vice versa.
Syntax – string.swapcase()
Parameter Values – No.
Example: Returns the swapped-case of the string.
text = "i read articles on codingstreets."
swap_case = text.swapcase()
print(swap_case)
#output:
I READ ARTICLES ON CODINGSTREETS.
Example: Returns the swapped-case of the string.
text = "I READ ARTICLES ON CODINGSTREETS."
swap_case = text.swapcase(
print(swap_case)
#output:
i read articles on codingstreets.
title() method
title() returns the string where first letter of each word is upper-case.
Example: Returns the each letter of the first word in upper-case.
text = "I READ ARTICLES ON CODINGSTREETS."
title_case = text.title()
print(title_case)
#output:
I Read Articles On Codingstreets.
Example: Returns the each letter of the first word in upper-case, when string contains number and special character.
text = "I REA1) ARTICLE5 ON CODINGSTREETS."
title_case = text.title()
print(title_case)
#output:
I Rea1) Article5 On Codingstreets.
Example: Returns the each letter of the first word in upper-case, when string contains number and special character.
text = "I R3AD ART1CLE5 ON C0D!NGSTREETS."
title_case = text.title()
print(title_case)
#output:
I R3Ad Art1Cle5 On C0D!Ngstreets.
translate() method
translate() returns the translated string after the specified character is translated to the specified character.
Syntax – string. translate(table)
Parameter Values –
table – Requiring either a dictionary or mapping table how to replace character.
Example: Returns the translated string.
text = "I READ ARTICLE5 ON CODINGSTREETS"
translates = text.maketrans('5','S')
print(text.translate(translates))
#output:
I READ ARTICLES ON CODINGSTREETS
Example: Use mapping table to replace original characters from string.
text = "Jello World"
text1 = "Jello" # will be replaced by variable text2.
text2 = "Hello"
translates = text.maketrans(text1, text2)
print("Translated String:",text.translate(translates))
#output:
Translated String: Hello World
Example: Use mapping table to replace original characters from string.
text = "Jello World!"
text1 = "Jello"
text2 = "Hello"
text3 = "!" # will be removed.
translates = text.maketrans(text1, text2, text3)
print("Translated String:",text.translate(translates))
#output:
Translated String: Hello World
upper() method
upper returns the upper-case string.
Note: It ignores symbols and numbers.
Syntax – string.upper()
Parameter Values – No
Example: Converts the string in upper case.
text = "hello world"
upper_case = text.upper()
print(upper_case)
#output:
HELLO WORLD
Example: Converts the string in upper case but ignores the number and symbol.
text = "5ello 4orld!"
upper_case = text.upper()
print(upper_case)
#output:
5ELLO 4ORLD!
zfill() method
zfill() fills the string with zeros from beginning of the string, until it reaches the specified length.
If parameter (len) is less than string length, then no filling will be done.
Syntax – string.zfill(value)
Parameter Values –
len – Required. Required number of zeros to fill the length of string.
Example: Returns original string as its length is greater than specified zfill argument.
text1 = "Hello"
print(text1.zfill(3))
#output:
Hello
Example: Fills the string until it reaches to character 6.
text1 = "1"
text2 = "Hello"
print(text1.zfill(7))
print(text2.zfill(7))
#output:
0000001
00Hello
Conclusion
In conclusion, understanding and utilizing Python string methods can greatly enhance your ability to manipulate and transform text data. The string methods discussed in this article—strip(), swapcase(), title(), translate(), upper(), and zfill()—each serve specific purposes.
By familiarizing yourself with these Python string methods and their respective functionalities, you can effectively manipulate text data to suit your needs, streamline processing tasks, and achieve more accurate and consistent results in your projects.