Python String Methods Flashcards
Returns the whole thing to uppercase
PARAMETERS?
@.upper() - Returns the whole thing to uppercase
txt = “Hello my friends”
x = txt.upper()
print(x)
@output - HELLO MY FRIENDS
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
no parameters
Return true if all characters are uppercase
PARAMETERS?
@.isupper() - Return true if all characters are uppercase
txt = “THIS IS NoW!”
x = txt.isupper()
print(x) o print(txt.isupper())
@output - False (Not every character is uppercase, o)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
No Parameters
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
text = “HELLO”
if text.isupper():
print(“All characters in the text are uppercase.”)
else:
print(“Not all characters in the text are uppercase.”)
Return true if all characters are lowercase
PARAMETERS?
@.islower() - Return true if all characters are lowercase
a = “Hello world!”
b = “hello 123”
c = “mynameisPeter”
print(a.islower())
print(b.islower())
print(c.islower())
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
no parameters
Returns the whole thing / string to lowercase
PARAMETERS?
@.lower() - Returns the whole thing to lowercase
txt = “Hello My FRIENDS”
x = txt.lower()
print(x)
@output - hello my friends
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
no parameters
Returns the whole string to lowercase AS WELL AS special characters from different languages like (ß, which is german)
@.casefold() - Returns string to lowercase as well as language characters
text = “Stra$ße” @ The German word for “street” with a special character
lower_text = text.lower()
casefolded_text = text.casefold()
print(“Original Text:”, text)
print(“Lowercase Text:”, lower_text)
print(“Casefolded Text:”, casefolded_text)
@output -
Original Text: Straße
Lowercase Text: straße
Casefolded Text: strasse
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
no parameters
Capitalizes the first letter of the string
@.capitalize() - Capitalizes the first letter
txt = “gojo”
x = txt.capitalize()
print (x)
@output - Gojo
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
NO PARAMETERS
Searches the string for a specified value and returns the first position of where it was found, could also do a range find (from _ to _)
@.find() - Tells the first time it is shown as index number
@RETURNS -1 IF VALUE NOT FOUND
@Can do a Range find where you can start and end the search wherever you want [x = txt.find(“e”, 5, 10)]
txt = “Hello, welcome to my world.”
x = txt.find(“e”)
print(x)
@output - 1, because the first e is shown at index 1
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
string.find(value, start, end)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
text = “Hello, World!”
substring = “World”
find outputs a -1 if the value isnt in the string, so we set it so that if its not equal to -1 we print that it is found and if it returns as -1 then it is not found since if it returns as -1 it really is not there
if text.find(substring) != -1 :
print(f”‘{substring}’ is found in the text.”)
else:
print(f”‘{substring}’ is not found in the text.”)
Searches the string for a specified value and returns the last occurrence / index of where it was found
@.rfind() - Tells / Returns the last time it was shown, shows the index(rfind = reversefind)
txt = “Mi casa, su casa.”
x = txt.rfind(“casa”)
print(x)
@output - 12
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
string.rfind(value, start, end)
This method finds the first occurrence / index number of the specified value and returns an ERROR if the value is not found.
@.index() - The index() method finds the first occurrence of the specified value and returns an ERROR[not a -1] if the value is not found.
txt = “Hello, welcome to my world.”
x = txt.index(“e”)
print(x)
@output - 1
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
string.index(value, start, end)
[][][][][][][][][][][][][][][][][][][][][][][][][][][]
txt = “Hello, welcome to my world.”
print(txt.find(“q”))
print(txt.index(“q”))
@output -
-1
ERROR / EXCEPTION
This method finds the last occurrence of the specified value and returns an ERROR[not a -1] if the value is not found.
@.rindex() - The rindex() method finds he last occurrence of the specified value and returns an ERROR[not a -1] if the value is not found.
txt = “Mi casa, su casa.”
x = txt.rindex(“casa”)
print(x)
@output - 12
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
string.index(value, start, end)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
txt = “I’m kool and this a really kool world!”
x = txt.rindex(“kool”, 3, 36) @RANGE TYPE SHI….
print(x)
@output - 27
Replaces placeholders with a value of your choosing
@.format() - Replaces placeholders with value of your choosing
name = “Alice”
age = 30
greeting = “Hello, my name is {} and I am {} years old.”.format(name, age)
print(greeting)
@output - Hello, my name is Alice and I am 30 years old.
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
string.format(value1, value2…)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
txt1 = “My name is {fname}, I’m {age}”.format(fname = “John”, age = 36)
txt2 = “My name is {0}, I’m {1}”.format(“John”,36)
txt3 = “My name is {}, I’m {}”.format(“John”,36)
Decimal Position Trick #1 [Used with the ______ method]
[:.NUMBERf]
value = 3.14159265
formatted_value = “[:.2f]”.format(value)
print(formatted_value)
@output - 3.14
2 floating decimal places away
Turns each word into a list item
@.split() - Turns each word into a list item
txt = “welcome to the jungle”
x = txt.split()
print(x)
@output - [‘welcome’, ‘to’, ‘the’, ‘jungle’]
SYNTAX
string.split(separator, maxsplit)
You can specify the separator, default separator is any whitespace.
When maxsplit is specified, the list will contain the specified number of elements plus one
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
txt = “apple&banana&cherry&orange”
setting the maxsplit parameter to 1, will return a list with 2 elements!
x = txt.split(“&”, 1)
print(x)
@output -
[‘apple’, ‘banana&cherry&orange’]
@Used hashtag as seperators in a list,
Turns all items in an iterable(Tuple, Dictionary, Set) into a string using a character u choose as a separator
@.join() - Turns all items in an iterable(Tuple, Dictionary, Set) into a string using a character u choose as a separator:
myTuple = (“John”, “Peter”, “Vicky”)
x = “&”.join(myTuple)
print(x)
@output - John&Peter&Vicky (Seperated with hashtags but you could just do a space or anything else)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
string.join(iterable)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
myDict = {“name”: “John”, “country”: “Norway”}
mySeparator = “TEST”
x = mySeparator.join(myDict)
print(x)
@output - nameTESTcountry
@for dictionaries it only outputs the keys not the values(name, country) not (John Norway)
This method removes any leading(beginning), and trailing(end) whitespaces.
@.strip() - The strip() method removes any leading(beginning), and trailing(end) whitespaces.
@You can specify which character(s) to remove, if not, any whitespaces will be removed.
txt = “ banana “
x = txt.strip()
print(“of all fruits”, x, “is my favorite”)
output - of all fruits banana is my favorite (I left paramater open but if u put a character in there it will remove it from the beginning and end)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX
string.strip(characters)