DEV Community

Cover image for Python in a nutshell [Part-1](Recap)
Anurag Pandey
Anurag Pandey

Posted on

Python in a nutshell [Part-1](Recap)

Recap

Before jumping to next part let us quickly recall what we have learned last time.
In last part we discussed about using print and input function to print and to get input from user respectively. We also studied about the help function which should by now become your best friend to get to information. We also learned about type function which could be used to get type of different objects.

The next question that might be coming to your mind is how to take input from the user and the print it.

>>> name = input("Enter your name: ")
Enter your name: Ramesh Kumar
>>> print(name)
Ramesh Kumar
Enter fullscreen mode Exit fullscreen mode

You might be thinking cool, what about adding some text for eg, printing,
Hello <name>, how are you?
For that we have two good solution and one crappy one.
Lets check out the crappy one first.

>>> name = input("Enter your name: ")
Enter your name: Ramesh Kumar
>>> print("Hello ", name, ",how are you?")
Hello Ramesh Kumar,how are you?
Enter fullscreen mode Exit fullscreen mode

See why this is crappy?Never do like this! It might be a pain for you later to make changes and even understand the output.

Old style:

>>> name = input("Enter your name: ")
Enter your name: Ramesh Kumar
>>> print("Hello {},how are you?".format(name))
Hello Ramesh Kumar,how are you?
Enter fullscreen mode Exit fullscreen mode

Here the {} acts as a place holder and is substituted by the variable name.Don't worry too much if you don't get it. Learn the syntax and try it.When you try yourself you understand better and may arise with many questions like, now if I have to put two variables in a sentence, say name and age how can I do that?

>>> name = input("Enter your name: ")
Enter your name: Ramesh Kumar
>>> age = input("Enter your age: ")
Enter your age: 55
>>> print("Hi, I am {} and I am {} years old".format(name, age))
Hi, I am Ramesh Kumar and I am 55 years old
Enter fullscreen mode Exit fullscreen mode

Here we have two placeholders and are substituted by the values of variable name and age respectively.

A problem with this is if the number of place holders get larger it might be confusing where to substitute which. And it is also harder to understand what the sentence is printing in one go.
The new style was introduced in Python3.6, called f-strings, which I believe stands for formatted strings.

New style:

>>> name = input("Enter your name: ")
Enter your name: Ramesh Kumar
>>> print(f"Hello {name},how are you?")
Hello Ramesh Kumar,how are you?
Enter fullscreen mode Exit fullscreen mode

The string is prefixed with 'f' and anything in {} is treated as a python object to say.

>>> name = input("Enter your name: ")
Enter your name: Ramesh Kumar
>>> print(f"Hello {name.upper()}")
Hello RAMESH KUMAR
Enter fullscreen mode Exit fullscreen mode

Now just try different things and if any doubts arises google it and if you couldn't solve it then comment below.And if you solve your doubt then too comment below with your question and findings so that others could learn.

Hope you enjoyed reading.

Takeaways:

  1. Practice with what you have learnt so far.
  2. Use f-strings more often.(Check your Python version, 3.6 or higher is required).

Top comments (0)