Python String Methods Flashcards

1
Q

capitalize()

A

The capitalize() method returns a string where the first character is upper case.

str.capitalize()

txt = "hello, and welcome to my world."
x = txt.**capitalize**()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

casefold()

A

The casefold() method returns a string where all the characters are lower case.

string.casefold()

txt = "Hello, And Welcome To My World!"
x = txt.**casefold**()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

center()

A

The center() method will center align the string, using a specified character (space is default) as the fill character.

string.center(length, character)

txt = "banana"
x = txt.**center**(20)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

count()

A

The count() method returns the number of times a specified value appears in the string.

string.count(value, start, end)

txt = "I love apples, apple are my favorite fruit"
x = txt.**count**("apple")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

encode()

A

The encode() method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used.

string.encode(encoding=encoding, errors=errors)

txt = "My name is Ståle"
x = txt.**encode**()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

endswith()

A

The endswith() method returns True if the string ends with the specified value, otherwise False.

string.endswith(value, start, end)

txt = "Hello, welcome to my world."
x = txt.**endswith**(".")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

expandtabs()

A

The expandtabs() method sets the tab size to the specified number of whitespaces.

string.expandtabs(tabsize)

txt = "H\te\tl\tl\to"
x = txt.**expandtabs**(2)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

find()

A

The find() method finds the first occurrence of the specified value.

The find() method returns -1 if the value is not found.

string.find(value, start, end)

txt = "Hello, welcome to my world."
x = txt.**find**("welcome")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

format()

A

The format() method formats the specified value(s) and insert them inside the string’s placeholder.

string.format(value1, value2…)

txt = "For only {price:.2f} dollars!"
print(txt.**format**(price = 49))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

format_map()

A

Formats specified values in a string

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

index()

A

The index() method finds the first occurrence of the specified value.

string.index(value, start, end)

txt = "Hello, welcome to my world."
x = txt.**index**("welcome")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

isalnum()

A

The isalnum() method returns True if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9).

string.isalnum()

txt = "Company12"
x = txt.**isalnum**()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

isalpha()

A

The isalpha() method returns True if all the characters are alphabet letters (a-z).

string.isalpha()

txt = "CompanyX"
x = txt.**isalpha**()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

isdecimal()

A

The isdecimal() method returns True if all the characters are decimals (0-9).

string.isdecimal()

txt = "\u0033" #unicode for 3
x = txt.**isdecimal**()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

isdigit()

A

The isdigit() method returns True if all the characters are digits, otherwise False.

string.isdigit()

txt = "50800"
x = txt.**isdigit**()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

isidentifier()

A

The isidentifier() method returns True if the string is a valid identifier, otherwise False.

string.isidentifier()

txt = "Demo"
x = txt.**isidentifier**()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

islower()

A

The islower() method returns True if all the characters are in lower case, otherwise False.

string.islower()

txt = "hello world!"
x = txt.**islower**()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

isnumeric()

A

The isnumeric() method returns True if all the characters are numeric (0-9), otherwise False.

string.isnumeric()

txt = "565543"
x = txt.**isnumeric**()
19
Q

isprintable()

A

The isprintable() method returns True if all the characters are printable, otherwise False.

string.isprintable()

txt = "Hello!\nAre you #1?"
x = txt.**isprintable**()
20
Q

isspace()

A

The isspace() method returns True if all the characters in a string are whitespaces, otherwise False.

string.isspace()

txt = " "
x = txt.**isspace**()
21
Q

istitle()

A

The istitle() method returns True if all words in a text start with a upper case letter, AND the rest of the word are lower case letters, otherwise False.

string.istitle()

txt = "Hello, And Welcome To My World!"
x = txt.**istitle**()
22
Q

isupper()

A

The isupper() method returns True if all the characters are in upper case, otherwise False.

string.isupper()

txt = "THIS IS NOW!"
x = txt.**isupper**()
23
Q

join()

A

text = [‘Python’, ‘is’, ‘a’, ‘fun’, ‘programming’, ‘language’] “ “.join(text)

‘Python is a fun programming language’

24
Q

ljust()

A

The ljust() method will left align the string, using a specified character (space is default) as the fill character.

string.ljust(length, character)

txt = “banana”

x = txt.ljust(20)
print(x, “is my favorite fruit.”)

25
Q

lower()

A

The lower() method returns a string where all characters are lower case.

string.lower()

txt = “Hello my FRIENDS”

x = txt.lower()
print(x)

26
Q

lstrip()

A

The lstrip() method removes any leading characters (space is the default leading character to remove)

string.lstrip(characters)

txt = “ banana “

x = txt.lstrip()
print(“of all fruits”, x, “is my favorite”)

27
Q

maketrans()

A

The maketrans() method returns a mapping table that can be used with the translate() method to replace specified characters.

string.maketrans(x, y, z)

txt = “Hello Sam!”

mytable = txt.maketrans(“S”, “P”)
print(txt.translate(mytable))

28
Q

partition()

A

The partition() method searches for a specified string, and splits the string into a tuple containing three elements.

string.partition(value)

txt = “I could eat bananas all day”

x = txt.partition(“bananas”)
print(x)

29
Q

replace()

A

The replace() method replaces a specified phrase with another specified phrase.

string.replace(oldvalue, newvalue, count)

txt = “I like bananas”

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

30
Q

rfind()

A

The rfind() method finds the last occurrence of the specified value.

The rfind() method returns -1 if the value is not found.

The rfind() method is almost the same as the rindex() method. See example below.

string.rfind(value, start, end)

txt = "Mi casa, su casa."
x = txt.**rfind**("casa")
31
Q

rindex()

A

The rindex() method finds the last occurrence of the specified value.

The rindex() method raises an exception if the value is not found.

The rindex() method is almost the same as the rfind() method. See example below.

string.rindex(value, start, end)

txt = "Mi casa, su casa."
x = txt.**rindex**("casa")
32
Q

rjust()

A

The rjust() method will right align the string, using a specified character (space is default) as the fill character.

string.rjust(length, character)

txt = “banana”

x = txt.rjust(20)
print(x, “is my favorite fruit.”)

33
Q

rpartition()

A

The rpartition() method searches for the last occurrence of a specified string, and splits the string into a tuple containing three elements.

string.rpartition(value)

txt = “I could eat bananas all day, bananas are my favorite fruit”

x = txt.rpartition(“bananas”)
print(x)

34
Q

rsplit()

A

The rsplit() method splits a string into a list, starting from the right.

string.rsplit(separator, maxsplit)

txt = “apple, banana, cherry”

x = txt.rsplit(“, “)
print(x)

35
Q

split()

A

The split() method splits a string into a list.

string.split(separator, maxsplit)

txt = “welcome to the jungle”

x = txt.split()
print(x)

36
Q

splitlines()

A

The splitlines() method splits a string into a list. The splitting is done at line breaks.

string.splitlines(keeplinebreaks)

txt = “Thank you for the music\nWelcome to the jungle”

x = txt.splitlines()
print(x)

37
Q

startswith()

A

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

string.startswith(value, start, end)

txt = “Hello, welcome to my world.”

x = txt.startswith(“Hello”)
print(x)

38
Q

strip()

A

The strip() method removes any leading (spaces at the beginning) and trailing (spaces at the end) characters

string.strip(characters)

txt = “ banana “

x = txt.strip()
print(“of all fruits”, x, “is my favorite”)

39
Q

swapcase()

A

The swapcase() method returns a string where all the upper case letters are lower case and vice versa.

string.swapcase()

txt = “Hello My Name Is PETER”

x = txt.swapcase()
print(x)

40
Q

title()

A

The title() method returns a string where the first character in every word is upper case. Like a header, or a title.

string.title()

txt = “Welcome to my world”

x = txt.title()
print(x)

41
Q

The translate() method returns a string where some specified characters are replaced with the character described in a dictionary, or in a mapping table.

string.translate(table)

#use a dictionary with ascii codes to replace 83 (S) with 80 (P):
mydict = {83: 80}
A

mydict = {83: 80}

txt = “Hello Sam!”
print(txt.translate(mydict))

42
Q

upper()

A

The upper() method returns a string where all characters are in upper case.

string.upper()

txt = “Hello my friends”

x = txt.upper()
print(x)

43
Q

zfill()

A

The zfill() method adds zeros (0) at the beginning of the string, until it reaches the specified length.

string.zfill(len)

txt = “50”

x = txt.zfill(10)
print(x)

44
Q
A