6 Manipulating Strings Flashcards

1
Q

What are escape characters?

A

They let yo use characters that are otherwise impossible to use in a string, e.g.:

\t Tab
\n Newline (line break)
\ Backslash

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

How do you indicate a raw string? What does it do?

A

> > > print(r’That is Carol's cat.’)
That is Carol's cat.

It ignores all excape characters and prints any backslahs used in the string

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

What areTriple Quotes useful for?

A

E.g. for multiline strings or multiline comments.

Also you don´t need escape characters like \n for a new line

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

How do string interpolation/ the modulo operator work?

A

> > > name = ‘Al’
age = 4000
‘My name is %s. I am %s years old.’ % (name, age)
‘My name is Al. I am 4000 years old.’

In the modulo operator the %s operator inside the string acts as a marker to be replaced by values following the string.

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

What is a benefit of the modulo operator?

A

It requires less time & typing

Also, values (e.g. integers) don´t have to be converted to string

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

What is the f´string

A

> > > name = ‘Al’
age = 4000
f’My name is {name}. Next year I will be {age + 1}.’
‘My name is Al. Next year I will be 4001.’

It is similar to the modulo operator, just using{} instead of the %s. Also, it is indicated by an f’ at the beginning of a string.

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

Name 4 useful string mehods

A

spam. upper()
spam. lower()
spam. isupper()
spam. islower()

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

What do .isupper() / .islower() do?

A

They return boolean values:
True
False

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

The isX() Methods 5x

A

isalpha() Returns True if the string consists only of letters

isalnum() Returns True if the string consists only of letters and numbers

isdecimal() Returns True if the string consists only of numeric characters
isspace() Returns True if the string consists only of spaces, tabs, and newlines

istitle() Returns True if the string consists only of words that begin with an uppercase letter followed by only lowercase letters

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

Further useful methos

A

startswith()
endswith()
‘Hello, world!’.startswith(‘Hello’)

join() -> ‘, ‘.join([‘cats’, ‘rats’, ‘bats’])
split() -> ‘My name is Simon’.split()

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

How can you keep asking someone to enter a password until he enters one only consisting of numbers and letters?

A
while True:
    print('Please enter your password.')
    password = input()
    if password.isalnum():
        break
    print('Password must consist of letters and numbers only')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Name 3x Text-Justify methods. What values go into the bracket?

A

justify(nrSpaces, ‘fillerCharacter’):

rjust()
ljust()
center()

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

How can you make this string appear 10 spaces to the right, with the filler spaces being ‘+’?

spam = ‘Hello’

A

spam.rjust(10, ‘+’)

‘+++++Hello’

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