CS 2 Flashcards

1
Q

What does string.strip() do? Also what other things that you can do?

A

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”)

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

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?

A

(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”)

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

What/how does replace() work?

A

A method replaces a specified phrase with another specified phrase.

txt = “I like bananas”

x = txt.replace(“bananas”, “apples”)

print(x)

I like apples

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

What are replace() , for both required/optional

A

(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.”

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

What string method remove spaces to the left of the string:

characters Optional. A set of characters to remove as leading characters

A

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

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

Whar method removes any trailing characters (characters at the end a string), space is the default trailing character to remove.

A

What does string.rstrip(characters) do

-
txt = “banana,,,,,ssqqqww…..”

x = txt.rstrip(“,.qsw”)

print(x)
banana

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

The method returns a copy of the specified list.

A

How does list.copy() work, not string use a list

fruits = [“apple”, “banana”, “cherry”]

x = fruits.copy()

print(x)
[‘apple’, ‘banana’, ‘cherry’]

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

What do JSON strings always have?

A

“They have double strings”

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