BongoBinary

Must to Know python string methods

By Aloyce Bahati1 year ago5 minutes read

While python has many data types, String is one which is mostly used type, its very important to know important string methods inorder to manipulate this data type efficiently, The following includes some of the important string methods in python which are must to know for every developer.

 

1. capitalize().

This method is used to convert the first character of a string to uppercase

>>> var = "hello world"
>>> var = var.capitalize()
>>> var
'Hello world'
>>> 

 

2. strip()

This method is used to remove spaces in python string.

>>> var = "  Hello there "
>>> var = var.strip()
>>> var
'Hello there'
>>> 

 

3. replace()

This method returns a string where the inputted value is replaced 

>>> var = "home coming"
>>> var.replace("home","school")
'school coming'
>>> 

will replace home to School

 

4.upper()

This method will replace the content of the string from lowercase to uppercase, it has no effect on the uppercased string content

>>> var = "please follow me on instagram @bongobinary, youtube @bongobinary , twitter and facebook @bongobinary"
>>> var.upper()
'PLEASE FOLLOW ME ON INSTAGRAM @BONGOBINARY, YOUTUBE @BONGOBINARY , TWITTER AND FACEBOOK @BONGOBINARY'
>>> 

 

5.casefold()

This method does the opposite of upper method.

when applying this method the string will be converted to lowercase.

>>> var = "PLEASE SUBSCRIBE TO MY YOUTUBE CHANNEL"
>>> var.casefold()
'please subscribe to my youtube channel'
>>> 

 

6. count()

This string method will return the number of times a specified value occurs.

>>> var = "hello, hello, friends"
>>> var.count("hell")
2
>>> 

 

7. endswith()

Returns true if a string ends with a specified value.

>>> var = "hello there"
>>> var.endswith("there")
True
>>> 

 

8. find()

Look for a particular value in string and return the position where its found

>>> var = "hello there friends"
>>> var.find("hello")
0
>>> 

 

9. isalpha()

Return true if string characters are alphabet

>>> var = "Hello"
>>> var.isalpha()
True
>>> 

 

10. isdigit()

Returns true when the string comprise of digits()

>>> var = "1234"
>>> var.isdigit()
True
>>> var = "Hi"
>>> var.isdigit()
False
>>> 

 

11. isidentifier()

Returns true if the string can possibly be and identifier

>>> var = "hi now"
>>> var.isidentifier()
False
>>> var = "hinow"
>>> var.isidentifier()
True
>>> 

 

12. islower()

Returns true if all characters in string are in lowercase

>>> var = "hello there"
>>> var.islower()
True
>>> var = "Hello there"
>>> var.islower()
False
>>> 

 

13.isupper()

Return true if all characters are uppercase in string.

>>> var = "hello there"
>>> var.isupper()
False
>>> var = "HELLO THERE"
>>> var.isupper()
True
>>> var = "Hello TheRe"
>>> var.isupper()
False
>>> 

 

Thanks for visiting this site, if you would like to learn about sqlite3 python module check on this link. PYTHON SQLITE3 MODULE TUTORIAL

 

Conclussion


I hope the presented string methods will be useful to you at some point. Thank you for paying a visit to our techsite. Please dont forget to like follow and subscribe at our social account.

Twitter , Instagram , Youtube and  Facebook @bongobinary.

Do you know any String methods which we forgot to mention here? please Give us your feedback on the comment section bellow or share with us on social media by tagging us @bongobinary