String Operations Flashcards
Pull out the first digit in a string
stringname[0]
Pull out the nth digit of a string
stringname[n]
Pull out the last digit of a string (using negative indexing)
stringname[-1]
find the length of a string
len(stringname)
slice the first three digits of a string
stringname[0:3]
get every second letter of a string
stringname[::2]
get every second letter of a string starting from the 4th letter to the 8th
stringname[3:8:2]
What is
‘1’ + ‘2’
‘12’
how would i replicate a string three times
3 * “string goes in here”
OR
3 * stringname
how do i make a new line in a printed string
\n
how to i add a tab in a printed string
\t
how do i put a backslash in a printed string
put two \
OR
print(r”string goes \ in here”)
where do you put an r for raw string
just before the first “ of the string
what is the general format for operations?
variable.operation()
convert stringname to uppercase letters
newstringname = stringname.upper()