CS 2 Flashcards
What does string.strip() do? Also what other things that you can do?
A string method that removes any leading, and trailing whitespaces.
Leading means at the beginning of the string, trailing means at the end.
You can specify which character(s) to remove, if not, any whitespaces will be removed.
-
txt = “ banana “
x = txt.strip()
print(“of all fruits”, x, “is my favorite”)
Other than removing leading and trailing spaces, what can you do with/how for string.strip(), if I don’t put anything in the strip?
(ig)You can specify which character(s) to remove, if not, any whitespaces will be removed.
string.strip(characters)
characters Optional. A set of characters to remove as leading/trailing characters
-
txt = “ banana “
x = txt.strip()
print(“of all fruits”, x, “is my favorite”)
What/how does replace() work?
A method replaces a specified phrase with another specified phrase.
txt = “I like bananas”
x = txt.replace(“bananas”, “apples”)
print(x)
I like apples
What are replace() , for both required/optional
(ig) string.replace(oldvalue, newvalue, count)
oldvalue Required. The string to search for
newvalue Required. The string to replace the old value with
count Optional. A number specifying how many occurrences of the old value you want to replace. Default is all occurrences
txt = “one one was a race horse, two two was one too.”
x = txt.replace(“one”, “three”, 2)
print(x)
three three was a race horse, two two was one too.”
What string method remove spaces to the left of the string:
characters Optional. A set of characters to remove as leading characters
What/how to use lstrip() method?
string.lstrip(characters), describe its parameters
-
txt = “ banana “
x = txt.lstrip()
print(“of all fruits”, x, “is my favorite”)
of all fruits banana is my favorite
Whar method removes any trailing characters (characters at the end a string), space is the default trailing character to remove.
What does string.rstrip(characters) do
-
txt = “banana,,,,,ssqqqww…..”
x = txt.rstrip(“,.qsw”)
print(x)
banana
The method returns a copy of the specified list.
How does list.copy() work, not string use a list
fruits = [“apple”, “banana”, “cherry”]
x = fruits.copy()
print(x)
[‘apple’, ‘banana’, ‘cherry’]
What do JSON strings always have?
“They have double strings”