Python String Functions (UC-Wk 2) Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

String method to remove leading and trailing white spaces

A

str.strip()

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

How to remove other leading or trailing characters (non whitespace) from string

A

str.strip(“*”) removes leading / trailing asterisk

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

Split out words that are separated by a char in a string (eg, a white space or comma”

A

s.split(‘ ‘) or s.split(‘,’) (or any char)

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

How to slice strings (sub-set) with lists

A

list = ‘Hello’; list[1:3] gives chars indexed to position 1 and 2 (3 is excl)

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

Check to see if substring (‘He”) is in the substring ‘Hello’

A

‘He’ in stringVar (where stringVar = ‘Hello’)

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

Get the index of where a substring starts in a string (eg, ‘el’ in ‘Hello’)

A

word.find(‘el’) (returns 1)

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

ex. insert Bob and Joe into string = ‘Jane Jill’

A

string2 = ‘Jane Jill { } { } ‘; string2.format(‘Bob’, ‘Joe’)

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

specify where you want to insert strings into a string using format function

A

str = ‘I love {1} and{0}’ ; allows you to re-arrange the strings you’re inserting

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