Python is great for string manipulation. Python’s string manipulation ability was definitely one of the reasons for its popularity initially. Python’s built-in methods lets you do most common string manipulations. Here are the String object methods in Python and how to use them.
1 | s = 'built-in methods to manipulate strings in Python' |
1. How to Check a String Ends With a Suffix?
Python String object’s method endswith is great to see if a string/text ends with a specific suffix. It returns True, when the string ends with the suffix, False otherwise.
1 2 3 4 5 | s = 'built-in methods to manipulate strings in Python' >s.endswith( 'Python' ) True >s.endswith( 'C++' ) False |
2. How to Check a String Starts With a Prefix?
Python String object also has a method startswith to see a string/text starts with a specific prefix. It returns True, when the string starts with the prefix, False otherwise.
1 2 3 | s = 'built-in methods to manipulate strings in Python' >s.startswith( 'built-in' ) True |
3. How to Find If A Substring Is in a String?
Often you want to check if a string contains a substring. The best way to do that is use ‘in’ statement in Python.
1 2 3 | s = 'is this substring in string?' 'substring' in s True |
4. How To Split a String in Python
Often you want to split a text or string using a delimiter and create a list of words in the string. Python’s split() method lets you split a string. By default, split uses consecutive whitespace in the string as a single separator and gets you a list.
1 2 3 | >s = 'How to Split a String in Python' >s.split() [ 'How' , 'to' , 'Split' , 'a' , 'String' , 'in' , 'Python' ] |
You can also specify a delimiter to split method. Here, we split the string with the letter ‘a’ as delimiter.
1 2 3 | >s = 'How to Split a String in Python' >s.split( 'a' ) [ 'How to Split ' , ' String in Python' ] |
5. How To Join a List of Strings into a String in Python?
Often one wants to join or concatenate list of strings into a single string. Python’s join() method can be used join strings. While joining we can also specify how we want to join with a delimiter.
1 2 3 | s = 'How to Join a list of strings?' s_list = s.split() " " .join(s_list) |
6. How To Remove Whitespaces in a String in Python?
Python has three related methods, strip(), lstrip(),rstrip() to remove whitespaces in strings. Let us see examples of using strip(), lstrip(), rstrip() here.
Let us create a text with white spaces in the beginning and in the end.
1 | s = ' Python strip examples ' |
strip() method removes both the whitespaces, the ones in the beginning and in the end
1 2 | >s.strip() 'Python strip examples' |
lstrip() method removes only the left whitespaces and thus leaves the one at the end of the string.
1 2 | >s.lstrip() 'Python strip examples ' |
rstrip() method removes only the right whitespaces and thus leaves the one at the beginning of the string.
1 2 | >s.lstrip() ' Python strip examples' |
7. How To Convert to Lower Case?
Python String objects’ lower() method is to convert a string into lower case letters.
1 2 3 | >s = 'How To Convert a String to Lower Case' >s.lower() 'how to convert a string to lower case' |
8. How To Convert to Upper Case?
Python String objects’ upper() method is for converting a string into upper case. Note all letters in the string will be capitalized.
1 2 3 | >s = 'How To Convert a String to Upper Case' >s.upper() 'HOW TO CONVERT A STRING TO UPPER CASE' |
9. How To Convert to Title Case?
Python’s title() method converts a string to a titlecased version, where the first letter of each word is capitalized, while the characters are lowercase.
1 2 3 | >s = 'how to convert a string to title case?' >s.title() 'How To Convert A String To Title Case?' |