String Flashcards

1
Q

Length of string

A

len(“ola”)

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

Concat string

A

i = ‘abra’
b = ‘cadabra’
print(a+b)

-> abracadabra

print(1 + ‘abra’)
TypeError: can only concatenate str (not “int”) to str

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

Char by index

A

name = ‘Nonna’
name[1]

will return 0

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

Get substring from a string

A

fullName = ‘Nonna Dzhabieva’
fistname = fullName[0:5]

firstname -> ‘Nonna’

you can slice from the end using negative index . Note we slice from -10 to -1 not to -1 to -10
lastName = fullName[-10:-1]

we can slice from beginning to a specific char by omitting first index
firstName = fullName[:5]

or from specific char to the very end of string
lastName = fullName[7:]

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

Immutable?

A

Cannot be changes after its created
To alter a string you have to create a new one

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

lower and uper

A

fullName = ‘Nonna Dzhabieva’
t = fullName.lower()
‘nonna dzhabieva’

k = fullName.upper()
‘NONNA DZHABIEVA’

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

Removing whitespaces

A

name = ‘ Nonna ‘
a = name.strip()
‘Nonna’

l = name.lstrinp(). - removes from left only
‘Nonna ‘

r = name.rstrip() - removes from left
‘ Nonna’

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

Starts and ends

A

name = ‘Nonna’
name.startswith(‘No’)
true

name.startswith(‘no’) - case sensetive
false

name.endswith(‘nna’)
true

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

Title and capitalize

A

str.capitalize(): Capitalizes the first character of the string.

str.title(): Converts the first character of each word to uppercase.

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

Find and index

A

str.find() / str.index(): Searches the string for a specified value and returns its position.

name = “Alan Dzhabiev”
alan.find(‘lan’)
1

alan.index(‘l’)
1

both are the same but index raises ValueError if substring is not found

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

Replace

A

str.replace(): Replaces a specified phrase with another specified phrase.

name = ‘Nonna Kurbanova’
name.replace(‘Kurbanova’, ‘Dzhabieva’)
‘Nonna Dzhabieva’

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

Split and join

A

str.split(): Splits the string into a list based on a separator. separator can be any char or white space

name = “Alan Dzhabiev”
name.split(‘ ‘)
[‘Alan’, ‘Dzhabiev’]

str.join(): Joins elements of an iterable (like a list) into a single string.

words = [‘Hello’, ‘world’, ‘Python’, ‘is’, ‘awesome’]
sentence = ‘ ‘.join(words)

Output: “Hello world Python is awesome”

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

Reading from console

A

input(“Whats your name?”)
Whats your name?Nonna
‘Nonna’

or you can just call
name = input()
without any prompt

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

Multiplying string

A

name = ‘Nonna’
name * 5
‘NonnaNonnaNonnaNonnaNonna’

you can multiply string by number
but cant do string * string
“Nonna” * “Nonna” -> error

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

Data casting

A

int(‘9’)
9

float(‘8.9’)
8.9

str(7)
‘7’

bool(1)
True
bool(6)
True
bool(-1)
True
bool(0)
false

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

Print format

A

egg = 5
bacon = 2

print(f”I don’t eat {egg} eggs every day and {bacon} bacon “)

17
Q

Return an integer that represents the Unicode Character passed into it

A

Print unicode of ‘A’
print(ord(‘A’))
# Print unicode of ‘5’ print(ord(‘5’))
# Print unicode of ‘$’ print(ord(‘$’)) Output:
65
53
36

18
Q
A