One of the basic data types and built in classes, provides useful methods to operate on strings
str.<method>(<string>)
<string>.<method>()
This is both a data type and a built in class. As such it can be
called as a class with methods that operates on strings as
str.<method>(<string>)
or directly on any str
type as <string>.<method>()
In python STRINGS ARE INMMUTABLE!!! meaning that any method applied to them, will return a new string without modifying the original
If you want to split elements in a string based on a delimiter. Typically
to output it into a list. You can do so with the self.split()
method
my_string = "some space separated\n words"
my_list = my_string.split(" ")
print(my_list) # ['some', 'space', 'separated', 'words']
You can count how many times a character or substring is repeated in a
string with the count()
method
string = "Hello World"
print(string.count("l")) # 3
You can use self.replace(<match>, <replacement>)
to replace a match
in a string with a new string
my_string = "My string"
my_string.replace("My", "Your") # "Your string"
You can use the builtin sort()
with a string to sort all it's elements
this will return a list
string = "bdca"
string = "".join(sorted(string))
print(string) # "abcd"
With the find()
method, you can find the index position of the first matched
substring starting from the left
You can also specify an initial position to start serching from, and and ending index to search up until to
string = 'Hello there'
print(string.find('e')) # 1
print(string.find('e', 2)) # 8
This will turn the whole string into uppercase with self.upper()
- Turn a string into all upper case
start_text = "my silly sentence for examples."
str.upper(start_text)
# "MY SILLY SENTENCE FOR EXAMPLES."
- Or use directly on a str type
start_text = "my silly sentence for examples."
start_text.upper()
# "MY SILLY SENTENCE FOR EXAMPLES."
You can find if a string is lower case with self.isupper()
string = "UPPERCASE"
bool(string.isupper()) # True
You can capitalize a string or upper every word in a string with the
methods self.capitalize()
and self.title()
respectively
You can turn all characters in a string into lower case with self.lower()
string = "UPPERCASE"
string = string.lower() # "uppercase"
You can find if a string is lower case with self.islower()
string = "UPPERCASE"
bool(string.islower()) # False
For a simple string you can remove any and all extra white space at the
beginning and end of the string with self.strip()
If you just want to remove white space at the end of a string you can use the
self.rstrip()
method instead
string = " with spaces "
string.strip() # "with spaces"
You can turn a character string into it's corresponding ordinal number in
alphabetical order. You can do this with the ord()
built-in function.
This will turn a character into it's corresponding ASCII
identifier.
- To get it's value based on it's order on the alphabet you will have to subtract the output by 96
letter_position = ord('b') - 96
print(letter_position) # 2
You can also do the same process backwards with an integer with chr()
You can padd a string with zeros to the left as to ensure that the string is of a given length
- If you want your string to at least be
2
characters long you pass it as an argument
my_strings = ['2', '12', '122', 'a', '']
ensure_two = [string.zfill(2) for string in my_strings]
print(ensure_two) # ['02', '12', '122', '0a', '00']
You can combine strings and variables into a single string tempalte with
f string
, you have to prefix the string with f
and reference variables
with {}
my_variable_string = "world"
print(f"Hello {my_variable_string}!") # Hello world