Some of the most important string methods in python are :
- capitalize() : Convert the first letter of the given list an uppercase letter and returns a new string.
# String
s="cat"
# using capitalize()
z=s.capitalize()
print(z)
# Output : Cat
2.casefold() : Returns a string where all the elements are lower case.
# string
s=CAt
# using casefold()
z=s.casefold()
print(z)
# Output : cat
3.center() : Returns a string aligned by the given character spaces.
s=cat
z=s.center(10)
print(z)
# Output : cat
4.count() : Returns the number of times a given substring has occured in the given string.
s="catcat"
n=s.count('cat')
print(n)
# Output : 2
5.encode() : Encodes the string with the given encoding type.
txt = "My name is Sam."
print(txt.encode(encoding="utf-8",errors="ignore"))
# Output : My name is Sam.
6.endswith() : Returns False if the string does not end with the given substring.
txt="Apple"
print(txt.endswith('le'))
# Output : True
7.find(): Finds the first occurrence of the given substring.
txt='batballcat'
n=txt.find('cat')
print(n)
# Output : 7
8.format() : Formats the given value and insert them inside the string's placeholder.
txt = "I have {x} apples"
print(txt.format(x=20))
# Output : I have 20 apples.
9.format_map() : Formats the given dictionary value and insert the key and the value in the string's placeholder.
a = {'x':'John', 'y':'Doe'}
# Use of format_map() function
print("{x} {y} is a stranger.".format_map(a))
# Output : John Doe is a stranger.
10.index() : Returns the index occurrence of the first occurrence of the given substring.
a='batcatrat'
print(a.index('rat'))
# Output : 6
11.isalnum() : Checks whether all the characters in the string is alphanumeric or not.
a='batcatrat123'
print(a.isalnum())
# Output : True
12.isprintable() : Checks whether all the characters are printable or not.Also returns True if the list is empty.
a="Hello"
print(a.isprintable())
# Output : True
a="Hello\n"
print(a.isprintable())
# Output : False
13.isnumeric() : Checks whether all the characters are numeric or not.
a='1234'
print(a.isnumeric())
# Output : True
14.islower() : Checks whether all the characters are lowercase or not.
a='Bat'
print(a.islower())
# Output : False
15.isidentifier() : Checks whether the string is valid identifier or not.
a='1234_a'
print(a.isidentifier())
# Output : False
16.isdigit() : Checks whether all the characters are digit or not.
a='1234'
print(a.isdigit())
# Output : True
17.isdecimal() : Checks whether all the characters in the string are decimal or not.
a='12'
print(a.isdecimal())
# Output : True
18.isalpha() : Checks whether all the characters in the string are alphabets or not.
a='abd'
print(a.isalpha())
# Output : True
19.isspace() : Checks whether all the characters in the string are space or not.
a=' '
print(a.isspace())
# Output : True
20.zfill() : Places the given number of zeroes at the left side of string.
z='cat'
x=z.fill(3)
print(x)
# Output : 000cat
21.upper() : Converts all the lowercase characters to uppercase.
z='cAt'
print(z.upper())
# Output : CAT
22.title() : Converts the string to title case.
z='cAt'
print(z.title())
# Output : Cat
23.swapcase() : Converts all the uppercase characters to lowercase and vice versa.
z='cAt'
print(z.swapcase())
# Output : CaT
24.strip() : Removes the space from the beginning and the end of string
z=' cat '
print(len(z.strip()))
# Output : 3
25.startswith() : Checks whether the string starts with
the given substring.
z='cat'
print(z.startswith('c'))
# Output : True
26.splitlines() : Splits the string where each line ends and returns a list.
txt = "Cat\nMouse"
x = txt.splitlines()
print(x)
# Output : ['Cat', 'Mouse']
27.rpartition() : Returns a tuple with three elements.Takes a string as an input which becomes the middle element of the tuple.
txt = "catMousedog"
x = txt.rpartition("Mouse")
print(x)
# Output : ('cat', 'Mouse', 'dog')
28.rjust() : Right aligns the string by the given width.
txt = "cat"
print(txt.rjust(10))
# Output : cat
29.rfind() : Returns the highest index of the given substring.
txt='catbatcat'
print(txt.rfind('cat'))
# Output : 6
30.replace() : Replaces all the occurrence of the given substring with the desired substring and returns a new substring.
txt="bznznz"
a=txt.replace('z','a')
print(a)
# Output : banana
31.lstrip() : Removes the spaces from the beginning and returns a string.
txt=" cat "
l=txt.lstrip()
print(len(l))
# Output : 4
32.lower() : Converts all the uppercase letters to lowercase.
txt='CAt'
newtxt=txt.lower()
print(newtxt)
33.join() : Returns a concatenated string.
mytuple=('bat','cat','ball')
space=' '
x=space.join(mytuple)
print(x)
# Output : bat cat ball
Top comments (0)