1 Python Strings Flashcards

(14 cards)

1
Q

How can I convert all my text to lowercase?

A

text = “NLP is POWERFUL!”
print(text.lower())

Output:”nlp is powerful!”

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

My sentence has unwanted spaces at the beginning or end. How do I clear them?

A

text = “ clean me “
print(text.strip())

Output:
“clean me”

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

I want to remove commas or change emojis to words.

A

text = “I love NLP, AI, and ML 💡”
cleaned = text.replace(“,”, “”).replace(“💡”, “[idea]”)
print(cleaned)

Output:
“I love NLP AI and ML [idea]”

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

How do I break my sentence into words?

A

text = “I love machine learning”
words = text.split()
print(words)

Output:
[‘I’, ‘love’, ‘machine’, ‘learning’]

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

I cleaned my words. How do I put them back into a sentence?

A

words = [‘nlp’, ‘is’, ‘awesome’]
sentence = “ “.join(words)
print(sentence)

Output:
“nlp is awesome”

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

I want to know where a word starts in a sentence.

A

text = “Python is everywhere”
print(text.find(“is”)) # Returns index
print(text.index(“Python”)) # Returns 0

Output:
7
0

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

I want to know how many times a word or letter appears.

A

text = “data data data science”
print(text.count(“data”))

Output:
3

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

I want to grab the first 5 characters or last 3 letters.

A

text = “language”
print(text[:5]) # First 5: “langu”
print(text[-3:]) # Last 3: “age”

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

How do I check if my sentence starts or ends with something?

A

text = “Natural Language Processing”
print(text.startswith(“Natural”))
print(text.endswith(“Processing”))

Output:
True
True

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

I want to check if a string is just letters, numbers, both, or only space.

A

print(“AI”.isalpha()) # True
print(“2024”.isdigit()) # True
print(“AI2024”.isalnum()) # True
print(“ “.isspace()) # True

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

How many characters are in my string?

A

text = “hello”
print(len(text))

Output: 5

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

zfill(width)

A

id = “7”
print(id.zfill(4))

Output: “0007”

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

More on find()

A

s = “hello world”

Finds the position of the substring “world”
print(s.find(“world”))

Returns -1 when substring is not found
print(s.find(“python”))

Finds the position of “o” starting from index 5
print(s.find(“o”, 5))

Output:
6
-1
7

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

More on index()

A

s = “hello world”

Finds position of the substring “world”
print(s.index(“world”))

Raises a ValueError when substring is not found
print(s.index(“python”))

Finds position of “o” starting from index 5
print(s.index(“o”, 5))

Output:
6

ERROR!
Traceback (most recent call last):
File “<main.py>", line 7, in <module>
ValueError: substring not found</module></main.py>

7

Note:
Use find() when we need to check if a substring exists and do not want to handle exceptions.

Use index() when we are sure that the substring exists or we want an exception to be raised if it doesn’t exist.

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