1b. String Flashcards

1
Q

String indexing and slicing

A

Str [ start : stop : Step]
stop = “up to but not including”
stop can be longer than the len of itself

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

String reverse

A

Str [ : : -1]

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

String new line

A

\n

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

String “tab”

A

\t

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

how to break up a sentence into list of words

A

str. split() -> all instance

str. partition() -> first instance

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

F string with dictionary

A

Single quote outside then double quote inside

print(f’The variable is {dict[“key”]}’)

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

“in” String

A

name = “Zophie”
‘Zo’ in name => True
zo => False

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

multiline string

A

can quote it using ‘’’ ‘’’
str = ‘'’sdkfjskdfj
sdkfjskdfj
jsdkfjds’’’

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

raw string

A

r’This is Shelly's cat’

useful for a lot of backslash e.g. regular expression

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

string method

4 main type of str charater

A

mystr = ‘hello world!’

mystr. islower() => true (neglect space, false if only space)
mystr. isupper()
mystr. upper()
mystr. lower()

4 main type: num, alpha, space, special
isalpha
isalnum => alpha + num
isdecimal => no special, i.e. nataural number only
ispace => can be multiple spaces

.startswith
.endswith

join / split / partition

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

string alignment, trim, replace

A

‘Hello’.ljust(10)
‘Hello’.rjust(10, ‘-‘)
‘Hello.center(10, ‘=’)

’ Hello ‘.strip() / rstrip() / lstrip

“SpamSpamBaconSpamEggsSpam”.strip(‘ampS’)

‘Hello’.replace(‘e’, ‘XYZ’)

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

string formatting (insert variable)

A

%s
fstring
string.format:
print(‘{0:=<8} | {1:-^8} | {2:.>8}’.format(11,22,33))

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

String formatting 3 method

A

name = ‘Mary’
age = 18
mystr = ‘Hello %s, you are age %s’ % (name, age) => old
mystr2 = ‘Hello {}, you are age {}’.format(name, age)
mystr5 = f’Hello {name}, you are age {age}’
Hello Mary, you are age 18

mystr3 = 'Hello {1}, you are age {0}'.format(name, age)
mystr4 = 'Hello {b}, you are age {a}'.format(a=name, b=age)

Hello 18, you are age Mary

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

Number formatting with padding for int and floats

{a:=<8}

A

{ : “fill” alignment “total width” .length d/f}
alignment

a=11111
b=22
c=33
print(f’{a:=<8} | {b:-^8} | {c:.>8}’)

11111=== | —22— | ……33
***fill has to use with alignment

a=1.2355
print(f’{a:4.3}’) => 1.24
print(f’{a:4.3f}’) => 1.236

# integer numbers with minimum width
print("{:5d}".format(12))
   12
# width doesn't work for numbers longer than padding
print("{:2d}".format(1234))
1234
# padding for float numbers
print("{:8.3f}".format(12.2346))
  12.235
# integer numbers with minimum width filled with zeros
print("{:05d}".format(12))
00012
# padding for float numbers filled with zeros
print("{:08.3f}".format(12.2346))
0012.235

https://www.programiz.com/python-programming/methods/string/format

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

control h in string

A

Str.replace(‘a’,’b’)

Change A to b

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

print without line break

A

print(“abcde”, end=’’)

17
Q

joining string from a list

A

’, ‘.join([‘a’,’b’,’c’]) “note the element in the list must be string
a, b, c
‘\n’.join.join([‘a’,’b’,’c’])
or
str.join(“”, list) -> this is a string method

18
Q

counting

A

“Hello”.count(“l”)

19
Q

finding

A

“Hello”.find(“e”)