How to get rid of spaces when printing text and variables in python

后端 未结 2 454
别跟我提以往
别跟我提以往 2021-01-21 15:11

I want to make it so the user enters his name/age and it outputs the name and age in ten years. I have set it out like this:

    print(\"Let\'s find out how old          


        
2条回答
  •  终归单人心
    2021-01-21 16:00

    Better use string formatting:

    print('\n{} you will be {} in ten years.'.format(name, ageinten))
    

    or use sep='', but then you'd have to add trailing and leading spaces to the strings.:

    print("\n", name, " you will be ", ageinten, " in ten years.", sep='')
    

    Default value of sep is a space, that's why you're getting a space.

    Demo:

    >>> name = 'Example'
    >>> ageinten =  '20'
    >>> print("\n",name," you will be ",ageinten," in ten years.", sep='')
    
    Example you will be 20 in ten years.
    >>> print('\n{} you will be {} in ten years.'.format(name, ageinten))
    
    Example you will be 20 in ten years.
    

提交回复
热议问题