Strings Flashcards

1
Q

Ways to indicate a string.

A

’ ‘

” “

””” “””

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

Len(str) function

A

Determines the number of characters in a string.

> > > Len(‘pear’)
4

Empty string = ‘ ‘, “ “

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

String concatenation

A

’+’ = adds together strings

‘*’ = multiples the strings, ie concatenates many times

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

String definition

A

A sequence of one or more characters. Includes letters, numbers, punctuation, symbols, etc.

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

String indexing is used for what?

A

To access the individual characters of a string.

S= ‘apple’

S[0] = ‘a’

Square brackets are used to index strings.

Starts at “0”, 1, …

-1 is last character in negative indexing

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

What is the character encoding scheme used now?

A

Unicode

Replaced ASCII

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

ord(‘a’) = ?

A

Returns the character code for that character.

ord function

> > > ord(‘a’)
97

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

chr(97) = ?

A

Finds the character, given the character code

chr function

> > > chr(97)
‘a’

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

What are ‘whitespace characters ‘?

A

Characters that appear as blanks on the printed page.

\\ = \
\' = '
\" = "
\n = new lone
\r = return (carriage return)
\t = tab
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Slicing strings

A

How python lets you extract a substring from a string.

s[begin:end]

Starts at ‘begin ‘ and ends at (end - 1)

s[ :5] = from beginning to 4

s[5: ] = from 5 to end

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

String-testing functions

A

Test if a string has a certain form.
Returns a True or False
Also called Boolean functions or predicates

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

s.endswith(t)

A

String-testing functions, returns True when

s ends with string t

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

s.startswith(t)

A

String-testing functions, returns True when

s starts with string t

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

s.isalnum()

A

String-testing functions, returns True when

s contains only letters or numbers

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

s.isalpha()

A

String-testing functions, returns True when

s contains only letters

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

s.isdecimal()

A

String-testing functions, returns True when

s contains only decimal characters

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

s.isdigit()

A

String-testing functions, returns True when

s contains only digits

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

s.isidentifier()

A

String-testing functions, returns True when

s is a valid Python identifier (that is, name)

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

s.islower()

A

String-testing functions, returns True when

s contains only lowercase letters

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

s.isnumeric()

A

String-testing functions, returns True when

s contains only numeric characters

21
Q

s.isprintable()

A

String-testing functions, returns True when

s contains only printable characters

22
Q

s.isspace((

A

String-testing functions, returns True when

s contains only whitespace characters

23
Q

s.istitle()

A

String-testing functions, returns True when

s is a title-case string

24
Q

s.isupper()

A

String-testing functions, returns True when

s contains only uppercase letters

25
t in s
String-testing functions, returns True when s contains t as a substring
26
String-searching functions
Way to find substrings within a string Find - if not in string, returns a 'ValueError' Index - if not in string, returns a '-1'
27
s.find(t)
Returns-1, or index where t starts in s rfind(t) - searches right to left
28
s.index(t)
Returns ValueError, or index where t starts in s rindex(t) - searches right to left
29
s.capitalize()
s[ ] is made uppercase
30
s.lower()
All letters of s are made lowercase
31
s.upper()
All letters of s are made uppercase
32
s.swapcase()
Lowercase are made upper, and vice versa
33
s.title()
Title-case version of s
34
s.strip(ch)
Removes all ch characters in t occurring at the beginning or end of s
35
s.replace(old, new)
Replaces every occurrence of old within s with new
36
s.expandtabs(n)
Replaces each tab character in s with n spaces
37
s.count(t)
Number of times t occurs within s
38
s.encode()
Sets the encoding of s
39
s.join(seq)
Concatenates the strings in seq, using s as a separator
40
s.maketrans(old, new)
Creates a translation table used to change the characters in old with the corresponding characters in new
41
s.translate(table)
Makes the replacementsin s using the given translation table created with maketrans
42
s.zfill(width)
Adds enough '0's' to the left of s to make a string of length 'width '
43
Regular expressions
Is a way to compactly describe a set of strings.
44
What set of strings does xy? describe?
x, xy ? = character to the left of this is optional. 'Cats?' = cat, cats
45
What set of strings does 'x|y' describe?
x|y = x, y '|' = or
46
What set of strings does 'x*' describe?
x* = ' ', x, xx, xxx, xxxx, ...
47
What set of strings does 'x+' describe?
x+ = x,xx,xxx,xxxx, ....
48
What set of strings does '(ha)+!' Describe?
= ha!, haha!, hahaha! ...
49
What set of strings does 'ha+!' Describe?
= ha!, haa!, haaa!, ....