Python String Methods Flashcards

1
Q

Returns the whole thing to uppercase

PARAMETERS?

A

@.upper() - Returns the whole thing to uppercase

txt = “Hello my friends”

x = txt.upper()

print(x)

@output - HELLO MY FRIENDS
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
no parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Return true if all characters are uppercase

PARAMETERS?

A

@.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.”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Return true if all characters are lowercase

PARAMETERS?

A

@.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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Returns the whole thing / string to lowercase

PARAMETERS?

A

@.lower() - Returns the whole thing to lowercase

txt = “Hello My FRIENDS”

x = txt.lower()

print(x)

@output - hello my friends
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
no parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Returns the whole string to lowercase AS WELL AS special characters from different languages like (ß, which is german)

A

@.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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Capitalizes the first letter of the string

A

@.capitalize() - Capitalizes the first letter

txt = “gojo”

x = txt.capitalize()

print (x)

@output - Gojo
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
NO PARAMETERS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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 _)

A

@.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.”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Searches the string for a specified value and returns the last occurrence / index of where it was found

A

@.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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

This method finds the first occurrence / index number of the specified value and returns an ERROR if the value is not found.

A

@.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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

This method finds the last occurrence of the specified value and returns an ERROR[not a -1] if the value is not found.

A

@.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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Replaces placeholders with a value of your choosing

A

@.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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Decimal Position Trick #1 [Used with the ______ method]

A

[:.NUMBERf]

value = 3.14159265
formatted_value = “[:.2f]”.format(value)
print(formatted_value)

@output - 3.14

2 floating decimal places away

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Turns each word into a list item

A

@.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,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Turns all items in an iterable(Tuple, Dictionary, Set) into a string using a character u choose as a separator

A

@.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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

This method removes any leading(beginning), and trailing(end) whitespaces.

A

@.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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Returns True or False if every character is a digit

A

@.isdigit() - Returns True or False if every character is a digit

txt = “50800”

x = txt.isdigit()

print(x)

@output - True (Because all of the characters in txt are digits)
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
no parameters

user_input = input(“Enter a string: “)

if user_input.isdigit():
print(“The input consists of only digits.”)
else:
print(“The input contains non-digit characters.”)

17
Q

Returns True if all characters in string are letters(Spaces are included as characters)

A

+.isalpha() - Returns True if all characters in string are letters(Spaces are included as characters)

+output - False (Because there is a space)

txt = “The Owner”

x = txt.isalpha()

print(x)

18
Q

Returns the number of times it appears in the string

A

@.count() - Returns number of times it appears in the string

txt = “I love apples, apple are my favorite fruit”

x = txt.count(“apple”)

print(x)

@output - 2
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX -

string.count(value, start, end)
@RANGE

19
Q

This method returns True if the string begins with the specified value, otherwise False.

A

@.startswith() method returns True if the string starts with the specified value, otherwise False.

txt = “Hello, welcome to my world.”

x = txt.startswith(“Hello”)

print(x)

@output - True
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

SYNTAX

string.startswith(value, start, end)

[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]

txt = “Hello, welcome to my world.”

x = txt.startswith(“wel”, 7, 20)

print(x)

@output - True

20
Q

Returns true if the string ends with what u type

A

@.endswith() - Returns true if it end with what u type

txt = “My World”

x = txt.endswith(“d”)

print(x)

@output - True, because it ends in a d
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

string.endswith(value, start, end)

21
Q

Replace a word you choose from the string with a word you want(Can even replace something with spaces)

A

@.replace() - Replace a word you choose from the string with a word you want(Can even replace something with spaces)

txt = “I like bananas”

x = txt.replace(“bananas”, “apples”)

print(x)

@output - I like apples
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
SYNTAX

string.replace(oldvalue, newvalue, count)

@count - Optional. A number specifying how many occurrences of the old value you want to replace. Default is all occurrences

txt = “one one was a race horse, two two was one too.”

x = txt.replace(“one”, “three”, 2)

print(x)

@output - three three was a race horse, two two was ONE too.”

22
Q

Length of characters

A

@len() - Length of characters - Function

name = input(“Enter your full name: “)

result = len(name)

print(result) or print(len(name))

len(object) - SYNTAX

23
Q

Swaps all the characters {from lowercase to upper and vice versa}

A

@swapcase() - Swap upper and lower case characters from a string

txt = “Hello My Name Is PETER”

x = txt.swapcase()

print(x)

@output - hELLO mY nAME iS peter