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
The Amateur Coder
  • 789
  • 3
  • 11
  • 33
gabhsey
  • 15
  • 4

5 Answers5

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)
John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • 2
    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. – patrick7 Feb 14 '22 at 01:32
  • Thanks :D I added "\n" to keep them in same format in the output. – gabhsey Feb 14 '22 at 01:35
  • @alexpdev The question was edited after I answered to add "multiple lines" in the title. Before that, it wasn't clear. – John Gordon Jun 11 '22 at 04:48
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)
cottontail
  • 10,268
  • 18
  • 50
  • 51
Jogvi
  • 77
  • 1
  • 7
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))
Jack Deeth
  • 3,062
  • 3
  • 24
  • 39
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
BeRT2me
  • 12,699
  • 2
  • 13
  • 31
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}')