#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    1
    Rep Power
    0

    Question In python, how do you put variables in strings?


    It should be very simple, but I seem to be missing it. In perl it would look like this:

    $name = (whatever name you input)
    $age = (again, whatever you input previously)

    then you could display it like:

    print "Hello $name, you are $age years old."

    I know python could do

    print "Hello",name,"you are",age,"years old."

    But that doesnt cut it, since it always adds spaces.

    print "Hello",name,"!"

    whould print:

    Hello Jack !

    Dont ask me why python forces spaces in there.. is there anyway to just call variables within strings like in perl?
  2. #2
  3. No Profile Picture
    Python Prophet
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    Amersfoort, The Netherlands
    Posts
    45
    Rep Power
    14
    It is very simple: Just use string format characters.

    In this example:
    print 'Hello %s, you are %s years old.' % (name, age)

    You could also use string concatenation like this:
    print 'Hello '+name+', you are '+age+' years old.'
    But this is slower, and IMO less readable.
    Some people, when confronted with a problem, think I know, I'll use regular expressions. Now they have two problems. - Jamie Zawinski, in comp.lang.emacs
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0
    why can't you use %u instead of %s?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481

    user input


    User input from terminal changed between python2 and python3. This method works in both versions:
    Code:
    import sys
    
    sys.stdout.write('OK?  (y/n) ')
    user_input = sys.stdin.readline()
    if 'y' == user_input[0].lower():
        ok_action()
    else:
        no_go()
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65
    Code:
    name = 'Frank'
    age = 12
    
    # vars() is the local dictionary containing variables name and age as keys
    print("Hello %(name)s, you are %(age)s years old." % vars())
    
    ''' result -->
    Hello Frank, you are 12 years old.
    '''
    A little bit more old-fashioned:
    Code:
    import string
    
    name = "Frank"
    age = 12
    
    # variables to be substituted begin with a $
    t = string.Template("Hello $name, you are $age old!")
    # local disctionary vars() contains variables name and age
    s = t.substitute(vars())
    print(s)
    
    ''' result -->
    Hello Frank, you are 12 old!
    '''
    If you have Python273 or Python3, you can use:
    Code:
    name = 'Frank'
    age = 12
    
    # vars() is the local dictionary containing variables name and age as keys
    # needs Python273 or Python3 and higher
    print("Hello {name}, you are {age} years old.".format(**vars()))
    
    
    ''' result -->
    Hello Frank, you are 12 years old.
    '''
    Last edited by Dietrich; December 19th, 2012 at 02:51 PM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo