Day 2: Diving Deeper into Strings and Taking User Input

301次阅读
没有评论

Welcome back to your 100-day Python journey! On Day 1, you wrote your first program and learned about variables and comments. Today, we’ll explore one of the most common data types in more detail: strings. We’ll also learn how to make our programs interactive by accepting input from the user.

What Can You Do with Strings?

Strings are more than just static text. You can combine them, modify them, and use them to create dynamic messages. Let’s start with a fundamental operation: concatenation.

String Concatenation

Concatenation simply means joining strings together. In Python, you can use the + operator to combine strings.

Python

first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name)
# Output: John Doe

Notice that we had to add a space ("") between the first and last names to format the output correctly.

A Modern Way to Format Strings: f-Strings

While concatenation works, it can become clumsy when you have multiple variables. Python provides a more elegant and powerful way to format strings called f-strings (formatted string literals). To create an f-string, you simply put the letter f before the opening quote and place your variable names inside curly braces {}.

Python

first_name = "Jane"
last_name = "Doe"
profession = "developer"
message = f"Hello, my name is {first_name} {last_name}, and I am a {profession}."
print(message)
# Output: Hello, my name is Jane Doe, and I am a developer.

As you can see, f-strings are easier to read and write. They are the recommended way to format strings in modern Python.

Useful String Methods

Python strings come with many built-in “methods” that let you perform common tasks. A method is like a function that belongs to an object (in this case, a string). You call a method using a dot (.) after the variable name. Here are a few useful ones:

  • .title(): Capitalizes the first letter of each word in the string.
  • .upper(): Converts the entire string to uppercase.
  • .lower(): Converts the entire string to lowercase.
  • .strip(): Removes any leading or trailing whitespace.

Let’s see them in action:

Python

name = "eric idle"
print(name.title())      # Output:   Eric Idle
print(name.upper())      # Output:   ERIC IDLE
print(name.strip())      # Output: eric idle
print(name.strip().title()) # You can chain methods! Output: Eric Idle

Making Your Programs Interactive: The input() Function

So far, our programs have been static. The input() function lets us change that. It prompts the user for input and reads what they type, allowing you to store it in a variable.

The input() function always returns the user’s input as a string.

Python

name = input("What is your name?")
print(f"Hello, {name}! It's nice to meet you.")

When you run this code, it will pause and wait for you to type something and press Enter. Whatever you type will be stored in the name variable.

Today’s Exercise

Let’s combine everything we’ve learned today. Your task is to write a program that:

  1. Asks the user for their first name and stores it in a variable.
  2. Asks the user for their favorite color and stores it in another variable.
  3. Cleans up the user’s input by removing any extra whitespace and making sure the name is properly capitalized.
  4. Uses an f-string to print a custom message, like “Hello, Sarah! I see your favorite color is blue.”

Great job on completing Day 2! You’re now able to work with strings and create interactive programs. Tomorrow, we’ll start exploring numbers and the powerful mathematical operations you can perform with them. Keep coding!

正文完
 0
评论(没有评论)
验证码