9.5 String Methods & String Format¶
- “dot notation” is the way we connect the name of an object to the name of a method it can perform.
ss = "Hello, World"
print(ss.upper())
tt = ss.lower()
print(tt)
'''Output:
HELLO, WORLD
hello, world
'''
- In addition to
upper
andlower
, the following table provides a summary of some other useful string methods.
ss = " Hello, World "
els = ss.count("l")
print(els)
print("***" + ss.strip() + "***")
print("***" + ss.lstrip() + "***")
print("***" + ss.rstrip() + "***")
news = ss.replace("o", "***")
print(news)
'''Output:
3
***Hello, World***
***Hello, World ***
*** Hello, World***
Hell***, W***rld
'''
Zach’s Notes¶
- What I used to get all strings the same length (this was useful for adding numbers at the end and keeping them in line (SEE UNIT 5 ASSIGNMENT table_3 function for more info)
if len(m) <= 26:
m += ' '
9.5.1 String Format Method¶
What Zach Uses¶
- Abbreviated
format
in front of string and variable referenced in curly brackets.
a = 'world'
print(f"hello {a}")
Textbook¶
- The string method
format
, makes substitutions into places in a string enclosed in braces.
person = input('Your name: ')
greeting = 'Hello {}!'.format(person)
print(greeting)
- There can be multiple substitutions, with data of any type. Next we use floats. Try original price $2.50 with a 7% discount:
origPrice = float(input('Enter the original price: $'))
discount = float(input('Enter discount percentage: '))
newPrice = (1 - discount/100)*origPrice
calculation = '${} discounted by {}% is ${}.'.format(origPrice, discount, newPrice)
print(calculation)
'''Output:
$5.10 discounted by 6.1% is $4.7889.
'''
- The parameters are inserted into the braces in order.
- Format strings can give further information inside the braces showing how to specially format data.
- For two decimal places, put
:.2f
inside the braces for the monetary values:
origPrice = float(input('Enter the original price: $'))
discount = float(input('Enter discount percentage: '))
newPrice = (1 - discount/100)*origPrice
calculation = '${:.2f} discounted by {}% is ${:.2f}.'.format(origPrice, discount, newPrice)
print(calculation)
'''Output
$5.10 discounted by 6.1% is $4.79.
'''
- The 2 in the format modifier can be replaced by another integer to round to that specified number of digits.
Note: If you need curly braces in the string itself, use double {{
letter = """
Dear {0} {2}.
{0}, I have an interesting money-making proposition for you!
If you deposit $10 million into my bank account, I can
double your money ...
"""
print(letter.format("Paris", "Whitney", "Hilton"))
print(letter.format("Bill", "Henry", "Gates"))
'''Output:
Dear Paris Hilton.
Paris, I have an interesting money-making proposition for you!
If you deposit $10 million into my bank account, I can
double your money ...
Dear Bill Gates.
Bill, I have an interesting money-making proposition for you!
If you deposit $10 million into my bank account, I can
double your money ...
'''