Methods of python string

Methods of python string

Instructor-svgAl-Mamun Sarkar
Mar 24 , 2020

Functions of python string. Python capitalize(), lower(), upper(), count(), replace(), strip(), split() functions of strings.

 

String Methods of Python:

Method Description Example
capitalize() It converts the first character of the string to upper case txt.capitalize()
casefold() casefold method converts the string into lower case txt.casefold()
lower() Used to Converts a string into lower case txt.lower()
title() The first characters of all words will be upper case txt.title()
upper() All characters will be converted to upper case. txt.upper()
swapcase() The lower case will be the upper case and the Upper case will be lower case txt.swapcase()
format() Used to formats specified values in a string txt = "Product Priice is {price:.2f} tk."
txt.format(price = 49.23423)
join() Create a string from an iterable txt = ["Hello", "World"]
message = " ".join(txt)
replace() Used to replace a value with another value txt.replace("is", "are")
count() This is used  to get the number of times a specified value occurs in the string txt.count("is")
find() Used to search a value in the string. It returns the position. txt.find("welcome")
index() Used to search find the index of a value in a string txt.index("welcome")
rfind() Used to return the last position of where the value is found. txt.rfind("casa")
rindex() Used to return the last position of where the value is found. txt.rindex("are")
isalnum() Check if all characters are alphabet or numeric txt.isalnum()
isalpha() Check if all characters are in the alphabet txt.isalpha()
isdecimal() Check if all characters are decimals txt.isdecimal()
isdigit() Check if all characters are digits txt.isdigit()
islower() Check if all characters are lower case txt.islower()
isnumeric() Check if all characters are numeric txt.isnumeric()
isspace() Check if all characters are whitespaces txt.isspace()
istitle() Check if words start with Upper case txt.istitle()
isupper() Check if all characters are upper case txt.isupper()
startswith() Check if a string starts with the specified value txt.startswith("Welcome")
endswith() Returns true if the string ends with the given value passed as a parameter. txt.endswith(".")
center() This function is used to center a string txt.center(20),  txt.center(20, "*")
ljust() Used to Return a left justified version of the string txt.ljust(20), txt.ljust(20, "*")
rjust() Used to create right justified version of a string txt.rjust(20), txt.rjust(20, "*")
zfill() Fills with 0 values at the beginning of the string with a specified number. txt.zfill(10)
strip() Trim the string txt.strip()
lstrip() Used to trim left side of the string txt.lstrip()
rstrip() Used to trim right side of the string txt.rstrip()
split() Create list from a string at the specified separator txt = "a,b,c"
x = txt.split(",")
splitlines() Create list from s string at line breaks txt = """Hello boys.
How are you?
I am fine"""
x = txt.splitlines()
partition() Create a tuple where the string is parted into three parts txt = "Welcome to ArtofCSE"
txt.partition("to")

 

Modify Strings in Python using Mehods:

a = "Bangladesh is my country"
print(a.capitalize())
print(a.lower())
print(a.upper())

print(len(a))
print(a.count("d"))

print(a.replace("is", "are"))
print(a.strip('is'))

l = a.split(' ')
print(l[1])

 

Output:

Bangladesh is my country
bangladesh is my country
BANGLADESH IS MY COUNTRY
24
1
Bangladesh are my country
Bangladesh is my country
is

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram