Programming

Printing in Python : Formatting and Concatenation

In the previous two chapters we learned about functions and other cool stuff now lets revisit the stuff that matters the most, Output.

In this chapter we will explore the print command further.

Take a look at this simple program

name = raw_input('What is your name? ')
print name

Its clear that it prints whatever the user enters. Now lets make it a lil better.

name = raw_input('Enter your name: ')
print name + ' is a nice name'

Now the output will be:

Enter your name: Somdev
Somdev is a nice name.

That looks better right? So we used a + (plus) sign to combine a variable (name) and our string ( is a nice name). This method is known as Concatenation.

The sad thing is that you can only combine a string with a string. Let me show you:

The code may look fine but it gave an error because c variable holds a value which is an integer and we are trying to concatenate it with a string which is not possible.

Don’t worry! There’s another way to combine stuff.

name = raw_input('Enter your name: ')
print '%s is a nice name' % name

While the second line of the code gets executed, %s will be replaced with the name which is entered by the user in the first line.

  • %s is used for strings
  • %d is used for integers
  • %f is used for floats (decimal numbers)

Using the knowledge you gained just now, you can easily come up with a program like this:

name = raw_input('What is your name? ')
age = raw_input('What is your age? ')
age_10 = int(age) + 10
print '%s you will be %d years old after 10 years.' % (name, age_10)

Opps! I didn’t tell you that we can add two things in a string at the same time. Well adding two or more variables is pretty easy as you can see in the example above and make sure you write the variables on the right hand side in the order they appear in the print statement.

Output of this program is:

What is your name? Somdev
What is your age? 18
Somdev you will be 28 years old after 10 years.

A Really Basic Chatbot

print 'Hi! My name is JARVIS.'
name = raw_input('What is your name? ').lower()
print 'Nice to meet you %s' % name
gender = raw_input('What is your gender? ').lower()
if gender == 'male':
	print 'That\'s great %s. Most of my friends are boys' % name
	print 'Opps my mom is calling me. Bye'
if gender == 'female':
	bf = raw_input('Do you have a boyfriend %s? ' % name).lower()
	if bf == 'yes':
	    print 'I think you deserve a better one.'
	else:
		print 'I have two movie tickets so....'

The code must look familiar to you except the lower() thing. Well lower() is used to change something to lowercase like changing HaHaHa to hahaha. I used that because our program is really basic and so if the user enters Male instead of male, the if condition will not be satisfied. Gotcha?
Similarly upper() is used to convert strings to uppercase.

Try running and reading this ChatBot and build your own stuff using functions and formatting that we learned today.

Keep learning! Keep Coding!

Next Chapter >>

Also Read: What is Tor and how it works?


Subscribe Now

Subscribe for free and get latest articles delivered right into your inbox.

Thank you for subscribing.

Something went wrong.

Categories

>-----ADVERTISEMENT-----<