DEV Community

sanadiqbal
sanadiqbal

Posted on

PYTHON : STRING METHODS

Some of the most important string methods in python are :

  1. 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
Enter fullscreen mode Exit fullscreen mode

2.casefold() : Returns a string where all the elements are lower case.

# string
s=CAt
# using casefold()
z=s.casefold()
print(z)
# Output : cat
Enter fullscreen mode Exit fullscreen mode

3.center() : Returns a string aligned by the given character spaces.

s=cat
z=s.center(10)
print(z)
# Output :    cat
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

6.endswith() : Returns False if the string does not end with the given substring.

txt="Apple"
print(txt.endswith('le'))
# Output : True
Enter fullscreen mode Exit fullscreen mode

7.find(): Finds the first occurrence of the given substring.

txt='batballcat'
n=txt.find('cat')
print(n)
# Output : 7
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

10.index() : Returns the index occurrence of the first occurrence of the given substring.

a='batcatrat'
print(a.index('rat'))
# Output : 6
Enter fullscreen mode Exit fullscreen mode

11.isalnum() : Checks whether all the characters in the string is alphanumeric or not.

a='batcatrat123'
print(a.isalnum())
# Output : True
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

13.isnumeric() : Checks whether all the characters are numeric or not.

a='1234'
print(a.isnumeric())
# Output : True
Enter fullscreen mode Exit fullscreen mode

14.islower() : Checks whether all the characters are lowercase or not.

a='Bat'
print(a.islower())
# Output : False
Enter fullscreen mode Exit fullscreen mode

15.isidentifier() : Checks whether the string is valid identifier or not.

a='1234_a'
print(a.isidentifier())
# Output : False
Enter fullscreen mode Exit fullscreen mode

16.isdigit() : Checks whether all the characters are digit or not.

a='1234'
print(a.isdigit())
# Output : True
Enter fullscreen mode Exit fullscreen mode

17.isdecimal() : Checks whether all the characters in the string are decimal or not.

a='12'
print(a.isdecimal())
# Output : True
Enter fullscreen mode Exit fullscreen mode

18.isalpha() : Checks whether all the characters in the string are alphabets or not.

a='abd'
print(a.isalpha())
# Output : True
Enter fullscreen mode Exit fullscreen mode

19.isspace() : Checks whether all the characters in the string are space or not.

a='  '
print(a.isspace())
# Output : True
Enter fullscreen mode Exit fullscreen mode

20.zfill() : Places the given number of zeroes at the left side of string.

z='cat'
x=z.fill(3)
print(x)
# Output : 000cat
Enter fullscreen mode Exit fullscreen mode

21.upper() : Converts all the lowercase characters to uppercase.

z='cAt'
print(z.upper())
# Output : CAT
Enter fullscreen mode Exit fullscreen mode

22.title() : Converts the string to title case.

z='cAt'
print(z.title())
# Output : Cat
Enter fullscreen mode Exit fullscreen mode

23.swapcase() : Converts all the uppercase characters to lowercase and vice versa.

z='cAt'
print(z.swapcase())
# Output : CaT
Enter fullscreen mode Exit fullscreen mode

24.strip() : Removes the space from the beginning and the end of string

z='  cat  '
print(len(z.strip()))
# Output : 3
Enter fullscreen mode Exit fullscreen mode

25.startswith() : Checks whether the string starts with
the given substring.

z='cat'
print(z.startswith('c'))
# Output : True
Enter fullscreen mode Exit fullscreen mode

26.splitlines() : Splits the string where each line ends and returns a list.

txt = "Cat\nMouse"
x = txt.splitlines()
print(x)
# Output : ['Cat', 'Mouse']
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode

28.rjust() : Right aligns the string by the given width.

txt = "cat"
print(txt.rjust(10))
# Output :       cat
Enter fullscreen mode Exit fullscreen mode

29.rfind() : Returns the highest index of the given substring.

txt='catbatcat'
print(txt.rfind('cat'))
# Output : 6
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

31.lstrip() : Removes the spaces from the beginning and returns a string.

txt="  cat "
l=txt.lstrip()
print(len(l))
# Output : 4
Enter fullscreen mode Exit fullscreen mode

32.lower() : Converts all the uppercase letters to lowercase.

txt='CAt'
newtxt=txt.lower()
print(newtxt)
Enter fullscreen mode Exit fullscreen mode

33.join() : Returns a concatenated string.

mytuple=('bat','cat','ball')
space=' '
x=space.join(mytuple)
print(x)
# Output : bat cat ball
Enter fullscreen mode Exit fullscreen mode

Top comments (0)