CS: Python String Methods Flashcards
What are strings?
They are sequences of characters represented in a pair of quotation marks.
Python allow for the string to be in…
pairs of single or double quotes.
What is concatenating strings?
It is joining 2 strings using the + operator.
Ex: print(word1+word2)
How can you repeat a string over and over?
Using *
Ex: print(str*2)
What are the functions to convert string into uppercase characters? Give an example.
upper()
ex: print(text1.upper())
What are the functions to convert string into lowercase characters? Give an example.
lower()
ex: print(text1.lower())
How do you get the size of a string?
You get it by using the len() function.
ex: print(len(“Uni”))
ex: print(len(name))
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))
The text1 is: Python
Length of text is as follows:
6
The text2 is: Hello, Jon!
Length of text is as follows:
11
How do you count how many of a specific character is in a string? Give an example.
Using the word.count function.
Ex: print(potato.count(‘h’))
How do you replace a certain phrase with another in a string? Give an example.
Using the txt.replace function.
Ex: print(text.replace(“bananas”,”apples”)) -> banana gets replaced with apples
How do you capitalize the first letter?
Using the capitalize() function.
It also converts all the other characters to lowercase.
Ex:print(word.capitalize())
How do you print a specific letter from the string?
Using the string slice function.
Ex: print(word[0])
What does print(smthn[:]) do?
It prints the entire string
What does print(smthn[-5]) do?
It prints the 5th character from the right
Is (word[0]) going to print the first character?
Yes