Python String Methods Flashcards

1
Q

Converts the first character to upper case

A

capitalize()

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

Converts string into lower case

A

casefold()

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

Returns a centered string

A

center()

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

Returns the number of times a specified value occurs in a string

A

count()

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

Returns an encoded version of the string

A

encode()

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

Returns true if the string ends with the specified value

A

endswith()

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

Returns true if the string ends with the specified value

A

endswith()

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

Sets the tab size of the string

A

expandtabs()

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 position of where it was found

A

find()

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

Formats specified values in a string

A

format()

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

Formats specified values in a string

A

format_map()

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

Searches the string for a specified value and returns the position of where it was found

A

index()

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

Returns True if all characters in the string are alphanumeric

A

isalnum()

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

Returns True if all characters in the string are in the alphabet

A

isalpha()

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

Returns True if all characters in the string are ascii characters

A

isascii()

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

Returns True if all characters in the string are decimals

A

isdecimal()

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

Returns True if all characters in the string are digits

A

isdigit()

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

Returns True if the string is an identifier

A

isidentifier()

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

Returns True if all characters in the string are lower case

A

islower()

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

Returns True if all characters in the string are numeric

A

isnumeric()

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

Returns True if all characters in the string are numeric

A

isnumeric()

21
Q

Returns True if all characters in the string are printable

A

isprintable()

22
Q

Returns True if all characters in the string are whitespaces

A

isspace()

23
Q

Returns True if the string follows the rules of a title

A

istitle()

24
Q

Returns True if all characters in the string are upper case

A

isupper()

25
Q

Converts the elements of an iterable into a string

A

join()

25
Q

Converts the elements of an iterable into a string

A

join()

26
Q

Returns a left justified version of the string

A

ljust()

27
Q

Converts a string into lower case

A

lower()

28
Q

Returns a left trim version of the string

A

lstrip()

29
Q

Returns a translation table to be used in translations

A

maketrans()

30
Q

Returns a tuple where the string is parted into three parts

A

partition()

31
Q

Returns a string where a specified value is replaced with a specified value

A

replace()

32
Q

Searches the string for a specified value and returns the last position of where it was found

A

rfind()

33
Q

Searches the string for a specified value and returns the last position of where it was found

A

rindex()

34
Q

Returns a right justified version of the string

A

rjust()

35
Q

Returns a tuple where the string is parted into three parts

A

rpartition()

36
Q

Splits the string at the specified separator, and returns a list

A

rsplit()

37
Q

Returns a right trim version of the string

A

rstrip()

38
Q

Splits the string at the specified separator, and returns a list

A

split()

39
Q

Splits the string at line breaks and returns a list

A

splitlines()

40
Q

Returns true if the string starts with the specified value

A

startswith()

41
Q

Returns a trimmed version of the string

A

strip()

42
Q

Swaps cases, lower case becomes upper case and vice versa

A

swapcase()

43
Q

Converts the first character of each word to upper case

A

title()

44
Q

Returns a translated string

A

translate()

45
Q

Converts a string into upper case

A

upper()

46
Q

Fills the string with a specified number of 0 values at the beginning

A

zfill()

47
Q

Fills the string with a specified number of 0 values at the beginning

A

zfill()

48
Q

How do string methods affect the string?

A

All string methods returns new values. They do not change the original string.

49
Q

How do you assign strings to a variable?

A
  • Assigning a string to a variable is done with the variable name followed by an equal sign and the string:
    a = “Hello”
  • You can assign a multiline string to a variable by using three double or singe quotes:
    a = “"”Lorem ipsum dolor sit amet,
    consectetur adipiscing elit,
    sed do eiusmod tempor incididunt
    ut labore et dolore magna aliqua.”””
  • OR
    a = ‘'’Lorem ipsum dolor sit amet,
    consectetur adipiscing elit,
    sed do eiusmod tempor incididunt
    ut labore et dolore magna aliqua.’’’
50
Q

How would you check if a character, word or phrase is in a string?

A
  • To check if a certain phrase or character is NOT present in a string, we can use the keyword not in.
    txt = “The best things in life are free!”
    print(“expensive” not in txt)
  • OR
    txt = “The best things in life are free!”
    if “expensive” not in txt:
    print(“No, ‘expensive’ is NOT present.”)