Strings Flashcards
“Hello”
str
‘hello’
str
“ I don’t do that “
str
String is
Ordered sequences
Index and slicing str:
Used to grab sub sections of the string
Indexing allows grabbing a:
A section of a string.
Indexing notation uses:
[] Notation after the string (or variable assigned the string)
Indexing =
[] and a number index indicating position of what to grab
Character: h e l l o
Index: 0 1 2 3 4
Reverse index:
Index: 0 -4 -3 -2 -1
Slicing:
Allows grabbing a subset of multiple characters, a slice of string.
Slicing syntax:
[start:stop:step]
Slicing syntax: Start is:
Numerical index for the slice start
Slicing syntax: Stop is:
Stop Index goes up to (but not include the last character)
Slicing syntax: Step is:
Size of the “jump” you take.
Does white space count as characters in strings:
Yes
Strings are in what quotes:
“Words“ and ‘ word here’ or “You’re great”
Print “Hello world”
print(“Hello world”)
Add a line to a string
print(‘hello \n world’)
Add a tab to a print
print(‘hello \t world’)
Check length of string
len(’I am’) 4
- mystring = ‘abcdefghijk’
- Reverse this str by 2
- mystring[::-2]
- ‘kigeca’
- mystring = ‘abcdefghijk’
- grab only def
- mystring[:7]
- ‘abcdefg’
- mystring = ‘abcdefghijk’
- grab ‘bc’
- mystring[1:3]
- ‘bc’

