DEV Community

Ankit Dagar
Ankit Dagar

Posted on

String Methods of Python

1.capitalize() :-

It converts the first character to upper case
example:-

>>> astring='ankit'
>>> astring.capitalize()
'Ankit'
Enter fullscreen mode Exit fullscreen mode

2.count() :-

It gives the number of times a specified value occurs in a string
example:-

>>> astring='ankit dagar'
>>> astring.count(a)
>>> astring.count('a')
3
Enter fullscreen mode Exit fullscreen mode

3.endswith():-

It gives true if the string ends with the specified value
example:-

>>> filename = "example.txt"
>>> filename.endswith(".txt")
True
Enter fullscreen mode Exit fullscreen mode

4.find() :-

It searches the string for a specified value and returns the position of where it was found

>>> astring='ankit dagar'
>>> astring.find('k')
2
Enter fullscreen mode Exit fullscreen mode

5.format() :-

It formats the specified values in a string

>>> name = 'Ankit'
>>> age=22
>>> result="my name is {},i am {} year old".format(name,age)
>>> result
'my name is Ankit,i am 22 year old'
Enter fullscreen mode Exit fullscreen mode

6.index() :-

It searches the string for a specified value and returns the position of where it was found
example:-

>>> astring='ANKIT DAGAR'
>>>> astring.index('K')
2
Enter fullscreen mode Exit fullscreen mode

7.isdecimal() :-

It gives True if all characters in the string are decimals

>>> astring='ANKIT DAGAR'
>>>> astring.isdecimal()
False
Enter fullscreen mode Exit fullscreen mode

8.isdigit() :-

It gives True if all characters in the string are digits

>>> astring='ANKIT DAGAR'
>>> astring.isdigit()
False
Enter fullscreen mode Exit fullscreen mode

9.islower() :-

It gives True if all characters in the string are lower case

>>> astring='ANKIT DAGAR'
>>> astring.islower()
False
Enter fullscreen mode Exit fullscreen mode

10.isnumeric() :-

It gives True if all characters in the string are numeric

>>> astring='ANKIT DAGAR'
>>> astring.isnumeric()
False
Enter fullscreen mode Exit fullscreen mode

11.isspace() :-

It gives True if all characters in the string are whitespaces

>>> astring='ANKIT DAGAR'
>>> astring.isspace()
False
Enter fullscreen mode Exit fullscreen mode

12.istitle() :-

It gives True if the string follows the rules of a title

>>> astring='ANKIT DAGAR'
>>> astring.istitle()
False

Enter fullscreen mode Exit fullscreen mode

13.isupper() :-

It gives True if all characters in the string are upper case

>>> astring='ANKIT DAGAR'
>>> astring.isupper()
True
Enter fullscreen mode Exit fullscreen mode

14.lower() :-

It converts a string into lower case

>>> astring='ANKIT DAGAR'
>>> astring.lower()
'ankit dagar'
Enter fullscreen mode Exit fullscreen mode

References

GeeksforGeeks
W3Schools
freecodecamp

Top comments (0)