Input
Let’s write a program that depends on a value provided by the user. To take input from the user, Python provides us with the “input()” function which halts a program till a user provides input.
user_name = input("Enter your name: ") print("Welcome to Verification Master,", user_name)
The input() function has a parameter called “prompt”, which as the name suggests is a prompt that we can display when asking for input from the user. When Python reads our first line code, it would run the input function, then display the prompt we provided and wait till the user enters input. Keep in mind, the program will wait till you type an input, so once you’ve entered your value, press “Enter” to continue with the program. Python then stores our input into the “user_name” variable and then the program prints it out.
Output
In all of the previous blogs we have extensively used the “print()” function, so now let’s explore the different ways in which we can use a print statement. There are two other ways in which we can use the print statement, which Python developers have deemed as “Fancier Output Formatting”
Formatted string literals (in short, f-strings)
format() function
Working with the previous example, we can write the same code using f-strings as shown below:
user_name = input("Enter your name: ") print(f"Welcome to Verification Master, {user_name}")
As the short form suggests, we simply add an ‘f’ before the double quotes within the print function. This allows us to add the required variables within the double quotes itself, inside two curly brackets ‘{}’, and when we contrast this with how we’ve used print consistently this is a simpler way to write python code.
You might think this doesn’t make much of a difference but when working with larger codebases you will appreciate the ease that f-strings provide. Type out the following code to feel the difference for yourself:
user_name = "Rob" user_age = 31 user_profession = "actor" user_weight = 75.5 # Normal way print("Hi I am", user_name, "I am", user_age, "years old, I weigh about", user_weight, "kilos and I work as an", user_profession) # Using f-strings print(f"Hi I am {user_name}, I am {user_age} years old, I weigh about {user_weight} kilos and I work as an {user_profession}")
Using the format() function feels familiar to the normal way of writing a print statement but provides different ways to customize it.
print("Hi {}, welcome to {}".format("Christopher", "Verification Master"))
Try replacing “Christopher” with your own name, and then execute the program.
Let’s try customizing the above in a ridiculous way; in the above code, the list (“Christopher, “Verification Master”) is indexed as 0 and 1 instead of 1 and 2. This is a convention in all programming languages such that for a list of items indexing will begin at 0.
print("Hi {1}, welcome to {0}".format("Christopher", "Verification Master"))
Here, we specified that we want the value from index 1 (i.e., the second item) to be displayed where the first curly bracket is and the value from index 0 (i.e., the first item) to be displayed where the second curly bracket is, and this would give us the output:
We can also assign it a “key” and then when we call that particular key it will give the value, we have stored in it.
print(“Hi {name}, welcome to {website_name}”.format(website_name = “Verification Master”, name=”Christopher”))
This will give the output as:
Separators
Lastly, let’s talk about separators, which is a parameter within the print() function. If you look closer at our normal print statement you can notice a small feature. Consider the code we first used and look closer at the output:
user_name = input("Enter your name: ") print("Welcome to Verification Master,", user_name)
You can see that after the word “Master” we only have a comma, and we haven’t added extra space. Yet the output contains a space and then displays the name “Robert”. This is because of the separator parameter. In many of the functions we use or will use, there are parameters that we do not use, and are programmed with a default value that the function uses unless we explicitly say we want another value.
random_variable = 7 print("My preferred number is,", random_variable, sep=" ") # sep=" " is the default value
Upon execution, it displays:
random_variable = 7 print("My preferred number is,", random_variable, sep="#")
This gives the result as:
random_variable = 7 print("My preferred number is,", random_variable, sep="") # sep="" means no space
Hence the output will be:
What have we learned?
.How do we take a user’s input?
.What is a prompt (within the input function)?
.What are two ways to format our output?
.What is the short form of formatted string literals?
.How does Python index a list of values?
.What is the separator parameter within Python’s print function?
.How does Python handle parameters we do not use?