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