1

I'm new to programming and having trouble understanding Python. I would like to use only one print() to lessen its usage.

first_name = "Gabh" Gabh ="Musician" age = 21 height = 5.4 weight = 47 print(first_name + (" is a"), Gabh) print(("age:"), age) print(("height:"), height) print(("weight:"), weight) 

With this code, this is what I'm getting:

Gabh is a Musician age: 21 height: 5.4 weight: 47 
0

5 Answers 5

2

Use a single print() statement, with all the items separated by commas.

print(first_name, "is a", Gabh, "age:", age, "height:", height, "weight:", weight) 
Sign up to request clarification or add additional context in comments.

3 Comments

To add to John's answer, you can insert line breaks with a special string: "\n". This would help obtain your 4 row print statment instead of having 1 massive row.
Thanks :D I added "\n" to keep them in same format in the output.
@alexpdev The question was edited after I answered to add "multiple lines" in the title. Before that, it wasn't clear.
1

All you need to do is to use the '\n' command in the string. You might also want to use f-strings to make your code cleaner :)

print(first_name + (" is a"), Gabh, '\n', "age:", age, '\n',"height:", height, "\n","weight:",weight) 

Comments

0

If you'd like to have a preconfigured multiline block of text to print, and just add some values to it (bit like doing a mail-merge in Word), you can use the str.format method.

>>> help(str.format) format(...) | S.format(*args, **kwargs) -> str | | Return a formatted version of S, using substitutions from args and kwargs. | The substitutions are identified by braces ('{' and '}'). 

Multiline strings have """ (or, less commonly, ''').

template = """{name} is a {role}. Age: {age} Height: {height} metres Weight: {weight} milligrams""" gabh = template.format( name="Gabh", role="Musician", age=21, height=5.4, weight=47 ) print(gabh) 

(This is slightly different to f-strings, where values get put into the string at the moment it's created.)

If you have a dictionary with keys matching the {stuff} in {curly braces} in your template string, you can use format_map:

template = """{name} is a {role}. Age: {age} Height: {height} metres Weight: {weight} milligrams""" gabh = { "name": "Gabh", "role": "Musician", "age": 21, "height": 5.4, "weight": 47, } print(template.format_map(gabh)) 

Comments

0

This would be a nice clean way of writing everything with one print statement. Note specifically that print() has a possible sep argument that we can set to be a line break \n.

print(f'{first_name} is a {Gabh}', f'age: {age}', f'height: {height}', f'weight: {weight}', sep='\n') 

Output:

Gabh is a Musician age: 21 height: 5.4 weight: 47 

Comments

0
first_name = "Gabh" Gabh = "Musician" age = 21 height = 5.4 weight = 47 print(f'{first_name} is a {Gabh}\nage: {age}\nheight: {height}\nweight: {weight}') 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.