A string is a text value: a sequence of characters used to store and manipulate words, passwords, phone numbers, books, or digit characters. Write a specific string inside matching single or double quotation marks. section 4.3.4 section 3.3.2
Use strings to perform several practical programming tasks:
- Call string methods with dot notation, such as isupper(), isdigit(), isalnum(), and replace(). section 4.3.4
- Combine strings with +, which concatenates text rather than adding numbers. section 4.3.4 section 10.3.3
- Index strings to access individual characters, including digit characters in a code that must preserve a leading zero. section 10.3.3
- Pass strings into functions, return them, and test them with ordinary and edge-case inputs. section 6.2.1 section 3.3.3
Cleaning a phone number
A function can remove selected characters by repeatedly calling replace(): phone_number.replace('(', ''), then replacing ')' and '-'. The cleaned value remains a string. section 4.3.4
def clean_number(phone_number):
phone_number = phone_number.replace('(', '') #1
phone_number = phone_number.replace(')', '') #2
phone_number = phone_number.replace('-', '') #3
return phone_number
Inspect a complete string-cleaning function that replaces parentheses and hyphens with empty strings before returning the result. section 4.3.4























