Python String Manipulation and Graphs Flashcards
What is a raw string?
Suppresses blackslashes in string.
Make one by putting r before first quote e.g. r”some text”
It’s type is str just like regular strings
What is __doc__?
-If the first thing in a function or class body is a string literal, it is stored in the \_\_doc\_\_ attribute of the function/class -Can also be unicode strings
What data type does python not have?
character
Get the length of a string
– len(string)
Access a character in the string with subscript notation
– c = string[1]
● Access a substring of a string with slice notation
– sub = string[1:5]
● Reverse a string by using slice notation with a negative step
– rev = string[::-1]
● Iterate over the characters of a string
– for c in string:
Convert a string to an array of strings split on a given
separator
– arr = string.split(separator)
Concatenate an array of strings into a single string, inserting a separator between each
– s = separator.join(Iterable)
Standard approach to build a String Character-by-Character
The standard approach is to build an array of singlecharacter strings, then use join()
What are the string predicates?
Test type of characters in string; return true if nonempty and all characters are of the specified type
How do you capitalize the first character in a string?
cap = string.capitalize() first char uppercased, rest lowercased
How do you uppercase->lowercase and vice versa?
– swp = string.swapcase()
How do you capitalize the first character in every word?
– tc = string.title()