String Methods
String Methods
Python provides a set of built-in methods that we can use to alter and modify the strings.
upper() : The upper() method converts a string to upper case.
Example:
Output:
lower() : The lower() method converts a string to upper case.
Example:
Output:
strip() : The strip() method removes any white spaces before and after the string.
Example:
Output:
rstrip() : the rstrip() removes any trailing characters.
Example:
Output:
replace() : the replace() method replaces a string with another string.
Example:
Output:
split() : The split() method splits the give string at the specified instance and returns the separated strings as list items.
Example:
Output:
There are various other string methods that we can use to modify our strings.
capitalize() : The capitalize() method turns only the first character of the string to uppercase and the rest other characters of the string are turned to lowercase. The string has no effect if the first character is already uppercase.
Example:
Output:
center() : The center() method aligns the string to the center as per the parameters given by the user.
Example:
Output:
We can also provide padding character. It will fill the rest of the fill characters provided by the user.
Example:
Output:
count() : The count() method returns the number of times the given value has occurred within the given string.
Example:
Output:
endswith() : The endswith() method checks if the string ends with a given value. If yes then return True, else return False.
Example 1:
Output:
Example 2:
Output:
We can even also check for a value in-between the string by providing start and end index positions.
Example:
Output:
find() : The find() method searches for the first occurrence of the given value and returns the index where it is present. If given value is absent from the string then return -1.
Example:
Output:
As we can see, this method is somewhat similar to the index() method. The major difference being that index() raises an exception if value is absent whereas find() does not.
Example:
Output:
index() : The index() method searches for the first occurrence of the given value and returns the index where it is present. If given value is absent from the string then raise an exception.
Example:
Output:
As we can see, this method is somewhat similar to the find() method. The major difference being that index() raises an exception if value is absent whereas find() does not.
Example:
Output:
isalnum() : The isalnum() method returns True only if the entire string only consists of A-Z, a-z, 0-9. If any other characters or punctuations are present, then it returns False.
Example 1:
Output:
Example 2:
Output:
isalpha() : The isalnum() method returns True only if the entire string only consists of A-Z, a-z. If any other characters or punctuations or numbers(0-9) are present, then it returns False.
Example 1:
Output:
Example 2:
Output:
islower() : The islower() method returns True if all the characters in the string are lower case, else it returns False.
Example 1:
Output:
Example 2:
Output:
isprintable() : The isprintable() method returns True if all the values within the given string are printable, if not, then return False.
Example 1:
Output:
Example 2:
Output:
isspace() : The isspace() method returns True only and only if the string contains white spaces, else returns False.
Example 1:
Output:
Example 2:
Output:
istitle() : The istitile() returns True only if the first letter of each word of the string is capitalized, else it returns False.
Example 1:
Output:
Example 2:
Output:
isupper() : The isupper() method returns True if all the characters in the string are upper case, else it returns False.
Example 1:
Output:
Example 2:
Output:
replace() : The replace() method can be used to replace a part of the original string with another string.
Example:
Output:
startswith() : The endswith() method checks if the string starts with a given value. If yes then return True, else return False.
Example 1:
Output:
Example 2:
Output:
We can even also check for a value in-between the string by providing start and end index positions.
Example:
Output:
swapcase() : The swapcase() method changes the character casing of the string. Upper case are converted to lower case and lower case to upper case.
Example:
Output:
title() : The title() method capitalizes each letter of the word within the string.
Example:
Output: