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
Q
  • mystring = ‘abcdefghijk’
  • mystring[2:9:3]
A
  • ‘cfi’
26
Q
  • mystring = ‘abcdefghijk’
  • mystring[:7]
A
  • ‘abcdefg’
27
Q

Access a range of characters in a string, you need to slice a string:

A

slicing operator :

28
Q

S [start : stop : step]

A
  • Start position End position Increment
29
Q
  • S = ‘ABCDEFGHI’
  • print(S[2:7])
A
  • # CDEFG
30
Q
  • Slice with Positive & Negative Indices
  • S = ‘ABCDEFGHI’
  • print(S[2:-5])
A
  • # CD
31
Q
  • # Return every 2nd item between position 2 to 7
  • S = ‘ABCDEFGHI’
  • print(S[2:7:2])
A
  • # CEG
32
Q
  • # Returns every 2nd item between position 6 to 1 in reverse order
  • S = ‘ABCDEFGHI’
  • print(S[6:1:-2])
A
  • # GEC
33
Q
  • # Slice first three characters from the string
  • S = ‘ABCDEFGHI’
  • print(S[:3])
A
  • # ABC
34
Q
  • # Slice last three characters from the string
  • S = ‘ABCDEFGHI’
  • print(S[6:])
A
  • # GHI
35
Q
  • Str are:
A
  • Immutable
36
Q

.format() method

A
  • 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
Q
  • .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
A
  • 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
Q

Float formating Syntax: “{value:width.precisionf}”

A
  • 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
Q
  • f string format method
  • name = “Jose”

eg 2.

name = “Sam”

age = 3

A
  • 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
Q

Combine strings using

A

+ operator

>>> “pass” + “word”

‘password’

41
Q

multiply a string by a number using

A

* operator

>>> “Ha” * 4

‘HaHaHaHa’

42
Q

An “object” encapsulates two things:

A