Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 3rd, 2002, 03:39 PM
nkuzma nkuzma is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2002
Posts: 1 nkuzma User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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?

Reply With Quote
  #2  
Old February 4th, 2002, 06:31 AM
Taradino Taradino is offline
Python Prophet
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: Amersfoort, The Netherlands
Posts: 45 Taradino User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 12
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

Reply With Quote
  #3  
Old December 18th, 2012, 08:17 PM
PGFracing PGFracing is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 6 PGFracing User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 34 sec
Reputation Power: 0
why can't you use %u instead of %s?

Reply With Quote
  #4  
Old December 18th, 2012, 08:50 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
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!

Reply With Quote
  #5  
Old December 19th, 2012, 12:54 PM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 480 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 22 h 8 m 57 sec
Reputation Power: 63
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.
'''
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

Last edited by Dietrich : December 19th, 2012 at 01:51 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > In python, how do you put variables in strings?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap