Strings Flashcards
Ways to indicate a string.
’ ‘
” “
””” “””
Len(str) function
Determines the number of characters in a string.
> > > Len(‘pear’)
4
Empty string = ‘ ‘, “ “
String concatenation
’+’ = adds together strings
‘*’ = multiples the strings, ie concatenates many times
String definition
A sequence of one or more characters. Includes letters, numbers, punctuation, symbols, etc.
String indexing is used for what?
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
What is the character encoding scheme used now?
Unicode
Replaced ASCII
ord(‘a’) = ?
Returns the character code for that character.
ord function
> > > ord(‘a’)
97
chr(97) = ?
Finds the character, given the character code
chr function
> > > chr(97)
‘a’
What are ‘whitespace characters ‘?
Characters that appear as blanks on the printed page.
\\ = \ \' = ' \" = " \n = new lone \r = return (carriage return) \t = tab
Slicing strings
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
String-testing functions
Test if a string has a certain form.
Returns a True or False
Also called Boolean functions or predicates
s.endswith(t)
String-testing functions, returns True when
s ends with string t
s.startswith(t)
String-testing functions, returns True when
s starts with string t
s.isalnum()
String-testing functions, returns True when
s contains only letters or numbers
s.isalpha()
String-testing functions, returns True when
s contains only letters
s.isdecimal()
String-testing functions, returns True when
s contains only decimal characters
s.isdigit()
String-testing functions, returns True when
s contains only digits
s.isidentifier()
String-testing functions, returns True when
s is a valid Python identifier (that is, name)
s.islower()
String-testing functions, returns True when
s contains only lowercase letters
s.isnumeric()
String-testing functions, returns True when
s contains only numeric characters
s.isprintable()
String-testing functions, returns True when
s contains only printable characters
s.isspace((
String-testing functions, returns True when
s contains only whitespace characters
s.istitle()
String-testing functions, returns True when
s is a title-case string
s.isupper()
String-testing functions, returns True when
s contains only uppercase letters
t in s
String-testing functions, returns True when
s contains t as a substring
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’
s.find(t)
Returns-1, or index where t starts in s
rfind(t) - searches right to left
s.index(t)
Returns ValueError, or index where t starts in s
rindex(t) - searches right to left
s.capitalize()
s[ ] is made uppercase
s.lower()
All letters of s are made lowercase
s.upper()
All letters of s are made uppercase
s.swapcase()
Lowercase are made upper, and vice versa
s.title()
Title-case version of s
s.strip(ch)
Removes all ch characters in t occurring at the beginning or end of s
s.replace(old, new)
Replaces every occurrence of old within s with new
s.expandtabs(n)
Replaces each tab character in s with n spaces
s.count(t)
Number of times t occurs within s
s.encode()
Sets the encoding of s
s.join(seq)
Concatenates the strings in seq, using s as a separator
s.maketrans(old, new)
Creates a translation table used to change the characters in old with the corresponding characters in new
s.translate(table)
Makes the replacementsin s using the given translation table created with maketrans
s.zfill(width)
Adds enough ‘0’s’ to the left of s to make a string of length ‘width ‘
Regular expressions
Is a way to compactly describe a set of strings.
What set of strings does xy? describe?
x, xy
? = character to the left of this is optional.
‘Cats?’ = cat, cats
What set of strings does ‘x|y’ describe?
x|y = x, y
’|’ = or
What set of strings does ‘x*’ describe?
x* = ‘ ‘, x, xx, xxx, xxxx, …
What set of strings does ‘x+’ describe?
x+ = x,xx,xxx,xxxx, ….
What set of strings does ‘(ha)+!’ Describe?
= ha!, haha!, hahaha! …
What set of strings does ‘ha+!’ Describe?
= ha!, haa!, haaa!, ….