Chapter 6- String Manipulation Flashcards

1
Q

Raw string

A

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

The r is inside the printer argument but outside of strong

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

Indexing strings

A
>>> spam = 'Hello world!'
>>> spam[0]
'H'
>>> spam[4]
'o'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Slicing strings

A

> > > spam = ‘Hello world!’
fizz = spam[0:5]
fizz
‘Hello’

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

In and not in strings

A
>>> 'Hello' in 'Hello World'
True
>>> 'Hello' in 'Hello'
True
>>> 'HELLO' in 'Hello World'
False
>>> '' in 'spam'
True
>>> 'cats' not in 'cats and dogs'
False
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Upper()

Lower()

A
Converts the entire strong into upper or lower 
>>> spam = 'Hello world!'
>>> spam = spam.upper()
>>> spam
'HELLO WORLD!'
>>> spam = spam.lower()
>>> spam
'hello world!'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Important about upper() and lower()
Also how do I adjust for users inputting
Great GrEat and great

A
They do not change the string value
Unless you have an assignment 
But the return is still a string
print('How are you?')
feeling = input()
if feeling.lower() == 'great':
    print('I feel great too.')
else:
    print('I hope the rest of your day is good.')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

IsUpper()

IsLower()

A

Returns a boolean value of true or false
return a Boolean True value if the string has at least one letter and all the letters are uppercase or lowercase, respectively.
»> ‘abc12345’.islower()
True

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

Startswith()

Endswith()

A

Return True. They are Boolean

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

Split()

A

The split() method does the opposite: It’s called on a string value and returns a list of string

By default, the string ‘My name is Simon’ is split wherever whitespace characters such as the space, tab, or newline characters are found

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

Join()

A

The join() method is useful when you have a LIST of strings that need to be joined together into a single string value. The join() method is called

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

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

ABC’.join([‘My’, ‘name’, ‘is’, ‘Simon’])

A

It will join the list with the value called as the middle point

MyABCnameABCisABCSimon

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

Remember that join()

A

Remember that join() is called on a string value and is passed a list value

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

Split() at something else besides white space

What happens to the value you set as the delimiter

A

> > > ‘MyABCnameABCisABCSimon’.split(‘ABC’)
[‘My’, ‘name’, ‘is’, ‘Simon’]
‘My name is Simon’.split(‘m’)
[‘My na’, ‘e is Si’, ‘on’]

It gets deleted

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

Spam.split(‘\n’)

A

Will split everywhere there is a new line

Don’t confuse this with white space

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

Rust()

ljust()

A

The rjust() and ljust() string methods return a padded version of the string they are called on, with spaces inserted to justify the text. The first argument to both methods is an integer length for the justified string.

The argument is how long you want the new string to be

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

R justify for string length 10
R justify for string length 20
“Hello World”

A
>>> 'Hello'.rjust(10)
'     Hello'
>>> 'Hello'.rjust(20)
'               Hello'
>>> 'Hello World'.rjust(20)
'         Hello World'
>>> 'Hello'.ljust(10)
'Hello     '
17
Q

An optional second argument to rjust() and ljust()

A

> > > ‘Hello’.rjust(20, ‘’)
****Hello’
‘Hello’.ljust(20, ‘-‘)
‘Hello—————’

18
Q

Center()

A

> > > ‘Hello’.center(20)
‘ Hello ‘
‘Hello’.center(20, ‘=’)
‘=======Hello========’

19
Q

Strip()
Lstrip()
Rsttip()

A
>>> spam = '    Hello World     '
>>> spam.strip()
'Hello World'
>>> spam.lstrip()
'Hello World '
>>> spam.rstrip()
'    Hello World'
20
Q

Strip() argument

A

You can specify what CHARACTERS you want to strip

> > > spam = ‘SpamSpamBaconSpamEggsSpamSpam’
spam.strip(‘ampS’)
‘BaconSpamEggs’

Spam=Smap=Spma

21
Q
Escape characters for 
Single quote 
Double quote
Tab
New line
Backslash
A
\'
\"
\tab
\n
\\
OR USE TRIPLE QUOTES