How can I print variable and string on same line in Python?

后端 未结 18 2099
面向向阳花
面向向阳花 2020-11-28 01:45

I am using python to work out how many children would be born in 5 years if a child was born every 7 seconds. The problem is on my last line. How do I get a variable to work

18条回答
  •  清酒与你
    2020-11-28 02:16

    Python is a very versatile language. You may print variables by different methods. I have listed below five methods. You may use them according to your convenience.

    Example:

    a = 1
    b = 'ball'
    

    Method 1:

    print('I have %d %s' % (a, b))
    

    Method 2:

    print('I have', a, b)
    

    Method 3:

    print('I have {} {}'.format(a, b))
    

    Method 4:

    print('I have ' + str(a) + ' ' + b)
    

    Method 5:

    print(f'I have {a} {b}')
    

    The output would be:

    I have 1 ball
    

提交回复
热议问题