What Is %S In Python?

What Is %S In Python?

Asked on March 13, 2019 in Python.
Add Comment


  • 1 Answer(s)
    #Python2
    name = raw_input("who are you?")
    print "hello %s" % (name,)
    
    #Python3+
    name = raw_input("who are you?")
    print("hello %s" % (name,))

    The %s token allows me to insert (and potentially format) a string. Notice that the %s token is replaced by whatever I pass to the string after the % symbol. Notice also that I am using a tuple here as well (when you only have one string using a tuple is optional) to illustrate that multiple strings can be inserted and formatted in one statement.

    Answered on March 27, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.