Question

expected result:

What is your name? *name*
So your name is 'name' huh?

code :

name = input("What is your name? ")
print('So your name is ' , 'name' ,'?')

i need help on the second line of my code!

Was it helpful?

Solution

Try this:

name = input("What is your name? ") 
print("So your name is '{0}'?".format(name))

To solve issues like this only both single ' and double " quotes are allowed in python strings.

Also note I've use the string.format() function to format the string otherwise it would be ugly and would look like:

print("So your name is '" + name + "'?")

OTHER TIPS

Use string formatting with double quotes:

>>> name="user3094915"
>>> print("So your name is '%s'" % name)
So your name is 'user3094915'

In general, you could use ", ', """ and ''' as string delimiters in Python. Whenever you need to put a quite in your string, simply delimit the string with a different type.

The other way is escaping your quotes with \, e.g. \' or \". This is the classic way, and almost every other programming language uses this idiom, which arguably makes your code more readable to non-Pythonists.

You can do so by adding \ before the quote.. Code:

name = input('what is your name?')
print ('so yourname is \'',name,'\'')

or

print ('so yourname is \'{0}\''.format(name))  

Hope this helps... :)

Using the representation function repr gives nicer results:

for name in ("ocean","guiovar'ch"):
    print ("so yourname is '{0!s}'".format(name))
    print ("so yourname is {0!r}".format(name))
    print ("so yourname is %r" % name)

result

so yourname is 'ocean'
so yourname is 'ocean'
so yourname is 'ocean'
so yourname is 'guiovar'ch'
so yourname is "guiovar'ch"
so yourname is "guiovar'ch"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top