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.
This method is used to convert the first character of a string to uppercase
>>> var = "hello world" >>> var = var.capitalize() >>> var 'Hello world' >>>
This method is used to remove spaces in python string.
>>> var = " Hello there " >>> var = var.strip() >>> var 'Hello there' >>>
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
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' >>>
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' >>>
This string method will return the number of times a specified value occurs.
>>> var = "hello, hello, friends" >>> var.count("hell") 2 >>>
Returns true if a string ends with a specified value.
>>> var = "hello there" >>> var.endswith("there") True >>>
Look for a particular value in string and return the position where its found
>>> var = "hello there friends" >>> var.find("hello") 0 >>>
Return true if string characters are alphabet
>>> var = "Hello" >>> var.isalpha() True >>>
Returns true when the string comprise of digits()
>>> var = "1234" >>> var.isdigit() True >>> var = "Hi" >>> var.isdigit() False >>>
Returns true if the string can possibly be and identifier
>>> var = "hi now" >>> var.isidentifier() False >>> var = "hinow" >>> var.isidentifier() True >>>
Returns true if all characters in string are in lowercase
>>> var = "hello there" >>> var.islower() True >>> var = "Hello there" >>> var.islower() False >>>
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
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.
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