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
Q

t in s

A

String-testing functions, returns True when

s contains t as a substring

26
Q

String-searching functions

A

Way to find substrings within a string

Find - if not in string, returns a ‘ValueError’

Index - if not in string, returns a ‘-1’

27
Q

s.find(t)

A

Returns-1, or index where t starts in s

rfind(t) - searches right to left

28
Q

s.index(t)

A

Returns ValueError, or index where t starts in s

rindex(t) - searches right to left

29
Q

s.capitalize()

A

s[ ] is made uppercase

30
Q

s.lower()

A

All letters of s are made lowercase

31
Q

s.upper()

A

All letters of s are made uppercase

32
Q

s.swapcase()

A

Lowercase are made upper, and vice versa

33
Q

s.title()

A

Title-case version of s

34
Q

s.strip(ch)

A

Removes all ch characters in t occurring at the beginning or end of s

35
Q

s.replace(old, new)

A

Replaces every occurrence of old within s with new

36
Q

s.expandtabs(n)

A

Replaces each tab character in s with n spaces

37
Q

s.count(t)

A

Number of times t occurs within s

38
Q

s.encode()

A

Sets the encoding of s

39
Q

s.join(seq)

A

Concatenates the strings in seq, using s as a separator

40
Q

s.maketrans(old, new)

A

Creates a translation table used to change the characters in old with the corresponding characters in new

41
Q

s.translate(table)

A

Makes the replacementsin s using the given translation table created with maketrans

42
Q

s.zfill(width)

A

Adds enough ‘0’s’ to the left of s to make a string of length ‘width ‘

43
Q

Regular expressions

A

Is a way to compactly describe a set of strings.

44
Q

What set of strings does xy? describe?

A

x, xy

? = character to the left of this is optional.

‘Cats?’ = cat, cats

45
Q

What set of strings does ‘x|y’ describe?

A

x|y = x, y

’|’ = or

46
Q

What set of strings does ‘x*’ describe?

A

x* = ‘ ‘, x, xx, xxx, xxxx, …

47
Q

What set of strings does ‘x+’ describe?

A

x+ = x,xx,xxx,xxxx, ….

48
Q

What set of strings does ‘(ha)+!’ Describe?

A

= ha!, haha!, hahaha! …

49
Q

What set of strings does ‘ha+!’ Describe?

A

= ha!, haa!, haaa!, ….