CS: Python String Methods Flashcards

1
Q

What are strings?

A

They are sequences of characters represented in a pair of quotation marks.

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

Python allow for the string to be in…

A

pairs of single or double quotes.

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

What is concatenating strings?

A

It is joining 2 strings using the + operator.
Ex: print(word1+word2)

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

How can you repeat a string over and over?

A

Using *
Ex: print(str*2)

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

What are the functions to convert string into uppercase characters? Give an example.

A

upper()
ex: print(text1.upper())

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

What are the functions to convert string into lowercase characters? Give an example.

A

lower()
ex: print(text1.lower())

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

How do you get the size of a string?

A

You get it by using the len() function.
ex: print(len(“Uni”))
ex: print(len(name))

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

What’s the output?
text1 = “Python”
text2 = “Hello, Jon!”
print (“The text1 is: “ + text1)
print(“Length of text is as follows:”)
print(len (text1))
print (“The text2 is: “ + text2)
print(“Length of text2 is as follows:”)
print(len (text2))

A

The text1 is: Python
Length of text is as follows:
6
The text2 is: Hello, Jon!
Length of text is as follows:
11

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

How do you count how many of a specific character is in a string? Give an example.

A

Using the word.count function.
Ex: print(potato.count(‘h’))

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

How do you replace a certain phrase with another in a string? Give an example.

A

Using the txt.replace function.
Ex: print(text.replace(“bananas”,”apples”)) -> banana gets replaced with apples

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

How do you capitalize the first letter?

A

Using the capitalize() function.
It also converts all the other characters to lowercase.
Ex:print(word.capitalize())

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

How do you print a specific letter from the string?

A

Using the string slice function.
Ex: print(word[0])

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

What does print(smthn[:]) do?

A

It prints the entire string

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

What does print(smthn[-5]) do?

A

It prints the 5th character from the right

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

Is (word[0]) going to print the first character?

A

Yes

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

How do you print certain index positions of characters inside strings?

A

Using the index function.
Ex: print(txt[6:8])
(It stops before the last index, which is 7 in our case.)