In this lesson we will cover accepting user input, as well as a more efficient way to join values together.
To recieve input from the user, you can use the input()
function. Here’s an example:
print("User inputted: " + input())
You’ll notice that when you run the code, it doesn’t immediately output text. That’s because you need to input text into the console first. Type something in, and press enter. You’ll then see the output.
You can put something inside the ()
to have it appear before the user’s input in the console:
print("User's name: " + input("What's your name? "))
This would output something like this:
What's your name? Kraggle09
User's name: Kraggle09
You can also use input()
for variables, like this:
name = input("What's your name? ")
print("Your name is " + name)
print("So, " + name + ", how are you?")
F-Strings
For a while now we’ve been using +
to join values together, but there’s a better option. Let’s look at an exampled:
name = "Kraggle09"
age = 16
mood = "happy"
print("Your name is " + name + ". You are " + str(age) + " years old, and are " + mood + ".")
As you can tell, longer combinations of values can get pretty lengthy.
F-strings are a more efficient alternative, and are pretty simple to use. Here’s an example:
name = "Kraggle09"
print(f"Your name is {name}")
We declare the f-string by putting f
directly before the first "
. Then, we can insert variables using {}
. So in the example I mentioned before, we can instead do this:
name = "Kraggle09"
age = 16
mood = "happy"
print(f"Your name is {name}. You are {age} years old, and are {mood}.")
Not only is this shorter, but it’s also more readable and has the added bonus of allowing us to directly insert intergers (and floats) without manually converting them.