String functions
Python offers a lot of functions that enable you to modify strings. For example, there are functions to transform all characters in a string to uppercase, obtain the length of a string, strip leading and trailing spaces in a string, etc.
Here is a brief description of some of the commonly used functions:
- capitalize() – capitalizes the first letter of a string.
- isdigit() – returns True if a string contains only digits.
- islower() – returns True if all alphabetic characters are in lowercase.
- len(string) – obtains the length of string.
- lower() – converts all uppercase letters to lowercase.
- upper() – converts all lowercase letters to uppercase.
- strip() – strips all leading and trailing whitespace characters.
- swapcase() – inverts the case of each alphabetic character.
Here are a couple of examples:
>>> newString='Hello world!' >>> len(newString) 12 >>> newString.isdigit() False >>> newString.islower() False >>> newString.lower() 'hello world!' >>> newString.swapcase() 'hELLO WORLD!'