All Python String function explained
Estimated Reading time: 12 minutes
Introduction
In this guide, you will learn every single function that is integrated in string
object. Read until the very end to become Python String master.
Contents
.capitalize()
Firstly, in Python we can use the capitalize()
function to make the first letter in the sentence capitalized.
Input
S = "hello world"
#Print cpitalized string
print(S.capitalize())
Output
Hello world
.casefold()
Secondly, there is the casefold()
function. This function allows user to decapitalize a capital letter(s) in a string object.
Input
S = "HELLO WORLD"
print(S.capitalize())
Output
hello world
.center()
Next, there is the center()
function. This function can be used to center text in python.
Additionally, the second argument can be used to insert specific letter in both letter before and letter after specific text.
Input
S = "BINARY FISH"
# Insert the total number of letter after the text is centered.
print(S.center(len(S)+4,"…"))
Output
……BINARY FISH……
.count()
The count()
function can be used to count the number of specific word or phrases in given string object. For example, count() function will return 2 if the word is banana
and ‘n’ is specified.
Input
S = "BANANA"
print(S.count("N"))
Output
2
.encode()
The encode()
function is used to encodes the specific string using specific encoding method.(Default is utf-8
).
Input
print('+-*÷'.encode('utf-8'))
Output
b'+-*\xc3\xb7'