Chapter 6- String Manipulation Flashcards Preview

Automate The Boring Stuff python > Chapter 6- String Manipulation > Flashcards

Flashcards in Chapter 6- String Manipulation Deck (21):
1

Raw string

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

The r is inside the printer argument but outside of strong

2

Indexing strings

>>> spam = 'Hello world!'
>>> spam[0]
'H'
>>> spam[4]
'o'

3

Slicing strings

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

4

In and not in strings

>>> 'Hello' in 'Hello World'
True
>>> 'Hello' in 'Hello'
True
>>> 'HELLO' in 'Hello World'
False
>>> '' in 'spam'
True
>>> 'cats' not in 'cats and dogs'
False

5

Upper()
Lower()

Converts the entire strong into upper or lower
>>> spam = 'Hello world!'
>>> spam = spam.upper()
>>> spam
'HELLO WORLD!'
>>> spam = spam.lower()
>>> spam
'hello world!'

6

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

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.')

7

IsUpper()
IsLower()

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

8

Startswith()
Endswith()

Return True. They are Boolean

9

Split()

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

10

Join()

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'

11

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

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

MyABCnameABCisABCSimon

12

Remember that join()

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

13

Split() at something else besides white space

What happens to the value you set as the delimiter


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

It gets deleted

14

Spam.split('\n')

Will split everywhere there is a new line
Don't confuse this with white space

15

Rust()
ljust()

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

16


R justify for string length 10
R justify for string length 20
"Hello World"

>>> 'Hello'.rjust(10)
' Hello'
>>> 'Hello'.rjust(20)
' Hello'
>>> 'Hello World'.rjust(20)
' Hello World'
>>> 'Hello'.ljust(10)
'Hello '

17

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

>>> 'Hello'.rjust(20, '*')
'***************Hello'
>>> 'Hello'.ljust(20, '-')
'Hello---------------'

18

Center()

>>> 'Hello'.center(20)
' Hello '
>>> 'Hello'.center(20, '=')
'=======Hello========'

19

Strip()
Lstrip()
Rsttip()

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

20

Strip() argument

You can specify what CHARACTERS you want to strip

>>> spam = 'SpamSpamBaconSpamEggsSpamSpam'
>>> spam.strip('ampS')
'BaconSpamEggs'

Spam=Smap=Spma

21

Escape characters for
Single quote
Double quote
Tab
New line
Backslash

\'
\"
\tab
\n
\\
OR USE TRIPLE QUOTES