Python String Methods Flashcards

1
Q

Removes leading and trailing whitespace (or specified characters).

A

strip()

"  hello  ".strip()  # Output: "hello"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Replaces a specified substring with another substring.

A

replace()

"hello world".replace("world", "Python")  # Output: "hello Python"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Splits a string into a list based on a delimiter.

A

split()

"a,b,c".split(",")  # Output: ["a", "b", "c"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Joins elements of an iterable into a string using a specified separator.

A

join()

", ".join(["a", "b", "c"])  # Output: "a, b, c"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Converts all characters in a string to lowercase.

A

lower()

"HELLO".lower()  # Output: "hello"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Converts all characters in a string to uppercase.

A

upper()

"hello".upper()  # Output: "HELLO"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Checks if a string starts with a specified substring.

A

startswith()

"hello".startswith("he")  # Output: True
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Checks if a string ends with a specified substring.

A

endswith()

"hello".endswith("lo")  # Output: True
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Checks if all characters in a string are digits.

A

isdigit()

"123".isdigit()  # Output: True
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Checks if all characters are alphanumeric (letters or numbers).

A

isalnum()

"abc123".isalnum()  # Output: True
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Checks if all characters in a string are whitespace.

A

isspace()

"   ".isspace()  # Output: True
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Formats a string using placeholders.

A

format()

"Hello, {}".format("Alice")  # Output: "Hello, Alice"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Pads the string with zeros to make it a specified length.

A

zfill()

"5".zfill(3)  # Output: "005"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Returns the first index of a specified substring or -1 if not found.

A

find()

"hello".find("e")  # Output: 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Returns the number of occurrences of a specified substring.

A

count()

"hello hello".count("hello")  # Output: 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly