Strings¶
String Methods¶
Changing Case Methods¶
Method for changing character cases:
Method |
Purpose |
---|---|
|
Converts the first character to upper case |
|
Converts a string into lower case |
|
Swaps cases, lower case becomes upper case and vice versa |
|
Converts the first character of each word to upper case |
|
Converts a string into upper case |
Try them out with this code:
1sample = "a long time Ago in a Galaxy far far away"
2
3print(sample.capitalize())
4print(sample.lower())
5print(sample.swapcase())
6print(sample.title())
7print(sample.upper())
Substring Methods¶
These methods return information about substrings in the string
Method |
Purpose |
---|---|
|
Returns the number of times a specified value occurs in a string |
|
Searches the string for a specified value and returns the position of where it was found |
|
Searches the string for a specified value and returns the position of where it was found |
|
Searches the string for a specified value and returns the last position of where it was found |
|
Searches the string for a specified value and returns the last position of where it was found |
Try them out with this code:
1sample = "banna"
2
3print(sample.count("a"))
4print(sample.find("a"))
5print(sample.index("a"))
6print(sample.rfind("a"))
7print(sample.rindex("a"))
String Comparison Methods¶
These methods evaluate a string and return a Boolean value
Method |
Purpose |
---|---|
|
Returns true if the string ends with the specified value |
|
Returns True if all characters in the string are alphanumeric |
|
Returns True if all characters in the string are in the alphabet |
|
Returns True if all characters in the string are ascii characters |
|
Returns True if all characters in the string are ASCII decimals |
|
Returns True if all characters in the string are digits |
|
Returns True if the string is an valid variable name |
|
Returns True if all characters in the string are lower case |
``isnumeric()` |
Returns True if all characters in the string can be used as numbers such as digits, fractions, subscripts, superscripts, Roman numerals, currency numerators etc. |
|
Returns True if all characters in the string are printable |
|
Returns True if all characters in the string are whitespaces |
|
Returns True if the string follows the rules of a title |
|
Returns True if all characters in the string are upper case |
|
Returns true if the string starts with the specified value |
Try them out with this code:
1print("Melancholy".endswith("ly"))
2print("R2D2".isalnum())
3print("C3PO".isalpha())
4print("X-wing".isascii())
5print("0456 784 907".isdecimal())
6print("0345".isdigit())
7print("first_name".isidentifier())
8print("Juxtaposition".islower())
9print("45".isnumeric())
10print("\nhello".isprintable())
11print(" ".isspace())
12print("Ethereal".istitle())
13print("Maelstrom".isupper())
14print("Luminescent".startswith("le"))
String Justification Methods¶
Justification adds ‘padding’ characters to a string so it becomes a set length.
Method |
Purpose |
---|---|
|
Will center align the string, using a specified character (space is default) as the fill character. |
|
Will left align the string, using a specified character (space is default) as the fill character. |
|
Will right align the string, using a specified character (space is default) as the fill character. |
Try them out with this code:
1sample = "word"
2
3print(sample.center(20))
4print(sample.center(20, "_"))
5print(sample.ljust(20))
6print(sample.ljust(20,"_"))
7print(sample.rjust(20))
8print(sample.rjust(20,"_"))
String Modification Methods¶
These methods will modify a string
Method |
Purpose |
---|---|
|
Converts the elements of an iterable into a string |
|
Returns the string with the leading whitespace characters (spaces, tabs, newlines) removed |
|
Returns a string where a specified value is replaced with a specified value |
|
Splits the string at the specified separator from right to left, and returns a list |
|
Returns the string with the trailing whitespace characters (spaces, tabs, newlines) removed |
|
Splits the string at the specified separator from left to right, and returns a list |
|
Splits the string at line breaks and returns a list |
|
Returns a trimmed version of the string with leading a trailing whitespace removed. |
Try them out with this code:
1sample = "A long time ago in a galaxy far far away"
2sample_list = ["Use", "the", "Force"]
3sample_whitespace = " Jawa "
4sample_lines = "Help me Obi Wan Kenobi\nYou are my only hope"
5
6print(" ".join(sample_list))
7print(sample_whitespace.lstrip())
8print(sample.replace("Force", "fork"))
9print(sample.rsplit(" ",3))
10print(sample_whitespace.rstrip())
11print(sample.split(" ",3))
12print(sample.rsplit(" "))
13print(sample_lines)
14print(sample_lines.splitlines())
15print(sample_whitespace.strip())