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’
- mystring = ‘abcdefghijk’
- mystring[2:9:3]
- ‘cfi’
- mystring = ‘abcdefghijk’
- mystring[:7]
- ‘abcdefg’
Access a range of characters in a string, you need to slice a string:
slicing operator :
S [start : stop : step]
- Start position End position Increment
- S = ‘ABCDEFGHI’
- print(S[2:7])
- # CDEFG
- Slice with Positive & Negative Indices
- S = ‘ABCDEFGHI’
- print(S[2:-5])
- # CD

- # Return every 2nd item between position 2 to 7
- S = ‘ABCDEFGHI’
- print(S[2:7:2])
- # CEG

- # Returns every 2nd item between position 6 to 1 in reverse order
- S = ‘ABCDEFGHI’
- print(S[6:1:-2])
- # GEC
- # Slice first three characters from the string
- S = ‘ABCDEFGHI’
- print(S[:3])
- # ABC
- # Slice last three characters from the string
- S = ‘ABCDEFGHI’
- print(S[6:])
- # GHI
- Str are:
- Immutable
.format() method
- A good way to format objects into your strings for print statements is with the string .format() method.
- Syntax: ‘String here { } then also { }’.format(‘something1’,’something2’)
- .format() example:
- Print the fox fox fox
- Print the quick brown fox
- Print “The quick brown fox’ using key words
- Print “The fox fox fox’ using key words
- print(‘The {0} {0} {0}’.format(‘fox’,’brown’,’quick’))
- print(‘The {2} {1} {0}’.format(‘fox’,’brown’,’quick’))
- print(‘the {q} {b} {f}’.format(f=’fox’,b=’brown’,q=’quick’))
- print(‘the {f} {f} {f}’.format(f=’fox’,b=’brown’,q=’quick’))
Float formating Syntax: “{value:width.precisionf}”
- Pricision is most important. It tells how far past the decimal point to go.
- The last number is rounded up or down depending on the next number being high or low
- result = 0.1287001287001287
- print(“The result was {r:1.5f}”.format(r=result))
- The result was 0.12870
- f string format method
- name = “Jose”
eg 2.
name = “Sam”
age = 3
- print(‘Hello, his name is {}’.format(name))
- f string method
- print(f’Hello, his name is {name}.’)
Hello, his name is Joe.
eg2
- print(f’{name} is {age} years old.’)
Same is 3 years old.
Combine strings using
+ operator
>>> “pass” + “word”
‘password’
multiply a string by a number using
* operator
>>> “Ha” * 4
‘HaHaHaHa’
An “object” encapsulates two things: