Strings Flashcards

1
Q

“Hello”

A

str

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

‘hello’

A

str

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

“ I don’t do that “

A

str

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

String is

A

Ordered sequences

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

Index and slicing str:

A

Used to grab sub sections of the string

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

Indexing allows grabbing a:

A

A section of a string.

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

Indexing notation uses:

A

[] Notation after the string (or variable assigned the string)

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

Indexing =

A

[] and a number index indicating position of what to grab

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

Character: h e l l o

A

Index: 0 1 2 3 4

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

Reverse index:

A

Index: 0 -4 -3 -2 -1

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

Slicing:

A

Allows grabbing a subset of multiple characters, a slice of string.

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

Slicing syntax:

A

[start:stop:step]

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

Slicing syntax: Start is:

A

Numerical index for the slice start

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

Slicing syntax: Stop is:

A

Stop Index goes up to (but not include the last character)

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

Slicing syntax: Step is:

A

Size of the “jump” you take.

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

Does white space count as characters in strings:

A

Yes

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

Strings are in what quotes:

A

“Words“ and ‘ word here’ or “You’re great”

18
Q

Print “Hello world”

A

print(“Hello world”)

19
Q

Add a line to a string

A

print(‘hello \n world’)

20
Q

Add a tab to a print

A

print(‘hello \t world’)

21
Q

Check length of string

A

len(’I am’) 4

22
Q
  • mystring = ‘abcdefghijk’
  • Reverse this str by 2
A
  • mystring[::-2]
  • ‘kigeca’
23
Q
  • mystring = ‘abcdefghijk’
  • grab only def
A
  • mystring[:7]
  • ‘abcdefg’
24
Q
  • mystring = ‘abcdefghijk’
  • grab ‘bc’
A
  • mystring[1:3]
  • ‘bc’
25
* mystring = 'abcdefghijk' * mystring[2:9:3]
* 'cfi'
26
* mystring = 'abcdefghijk' * mystring[:7]
* 'abcdefg'
27
Access a range of characters in a string, you need to slice a string:
slicing operator :
28
S [start : stop : step]
* Start position End position Increment
29
* S = 'ABCDEFGHI' * print(S[2:7])
* # CDEFG
30
* Slice with Positive & Negative Indices * S = 'ABCDEFGHI' * print(S[2:-5])
* # CD
31
* # Return every 2nd item between position 2 to 7 * S = 'ABCDEFGHI' * print(S[2:7:2])
* # CEG
32
* # Returns every 2nd item between position 6 to 1 in reverse order * S = 'ABCDEFGHI' * print(S[6:1:-2])
* # GEC
33
* # Slice first three characters from the string * S = 'ABCDEFGHI' * print(S[:3])
* # ABC
34
* # Slice last three characters from the string * S = 'ABCDEFGHI' * print(S[6:])
* # GHI
35
* Str are:
* Immutable
36
.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')
37
* .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'))
38
Float formating Syntax: "{value:width.precision*f*}"
* 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
39
* 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.
40
Combine strings using
+ operator ## Footnote \>\>\> "pass" + "word" 'password'
41
multiply a string by a number using
\* operator ## Footnote \>\>\> "Ha" \* 4 'HaHaHaHa'
42
An "object" encapsulates two things: