Python Reading
Key Ideas
- str type
- String concatenation operator: +
- Substring operators: in, not in
- String indexing, e.g. if mascot = "hawks", mascot[2]
- String comparison: <, <=, = =, >, >=, !=
- String slicing, e.g. mascot[1:3]
- Common methods:
- Determining length: len
- Related to case: upper, lower, capitalize
- Related to whitespace: strip, lstrip, rstrip
- Related to formatting: center, ljust, rjust
- Related to finding substrings: find, rfind, index, rindex
- Related to counting occurrences: count
- Useful constants:
- string.ascii_lowercase
- string.ascii_uppercase
- string.digits
- string.punctuation
- Online Documentation: See Section 4.7
Active Learning
- Construct simple examples in the Python shell to illustrate
that you understand the concepts above.
- Download and experiment with demo.py
until you understand it fully. Why can the alternate_case
function be called with either one or two arguments? How would
you explain what the alternate_case function does
to someone who knows nothing about computer science?
- Write a function named double_integer that repeatedly asks the
user to enter an integer until a non-negative integer is entered.
The function should then return twice the integer's value.
For example, the user might enter "apple", then "3.14", then "-34", then
"34". In this example, the function should return 68.
Bonus Material (Unrelated to Strings)