Python String Functions (UC-Wk 2) Flashcards
String method to remove leading and trailing white spaces
str.strip()
How to remove other leading or trailing characters (non whitespace) from string
str.strip(“*”) removes leading / trailing asterisk
Split out words that are separated by a char in a string (eg, a white space or comma”
s.split(‘ ‘) or s.split(‘,’) (or any char)
How to slice strings (sub-set) with lists
list = ‘Hello’; list[1:3] gives chars indexed to position 1 and 2 (3 is excl)
Check to see if substring (‘He”) is in the substring ‘Hello’
‘He’ in stringVar (where stringVar = ‘Hello’)
Get the index of where a substring starts in a string (eg, ‘el’ in ‘Hello’)
word.find(‘el’) (returns 1)
ex. insert Bob and Joe into string = ‘Jane Jill’
string2 = ‘Jane Jill { } { } ‘; string2.format(‘Bob’, ‘Joe’)
specify where you want to insert strings into a string using format function
str = ‘I love {1} and{0}’ ; allows you to re-arrange the strings you’re inserting