二手产品经理

二手产品经理

THIS IS RENO

String Processing - 36 Days - Learn Python Online for 100 Days

Record#

  1. Today I learned about string manipulation.
    1. string.lower() = converts all letters to lowercase.
    2. string.upper() = converts all letters to uppercase.
    3. string.title() = capitalizes the first letter of each word.
    4. string.capitalize() = capitalizes the first letter of the first word.
    5. string.strip() = removes leading and trailing whitespace.
  2. When manipulating strings, it is important to pay attention to the order of functions. For example, in the string = ' phone'.capitalize().strip(), the result after execution is 'phone', not 'Phone'. This is because capitalize() capitalizes the letter after removing whitespace.
  3. Today's exercise: Create an address book program that does not allow duplicates.
namelist = []
while True:
  firstname = input("First Name: ").title()
  lastname = input("Last Name: ").title()
  myname = f"{firstname} {lastname}"
  if myname not in namelist:
    namelist.append(myname)
    print(myname)
  else:
    print("ERROR: Duplicate name")
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.