User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 397,752 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,534 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Views: 30925 | Replies: 3
Reply
Join Date: Apr 2005
Location: Bangalore, India
Posts: 6
Reputation: s-sriram is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
s-sriram's Avatar
s-sriram s-sriram is offline Offline
Newbie Poster

Help string concatenate

  #1  
Apr 11th, 2005
Hi
I am trying my first try at Python. I want to do something very similar to what sprintf does with C. I want to concatenate a string with a number and store it into a new string.

I wrote a snippet like this

username='user'
for i in range (1-100)
user=user_name+str(i)
print user
else: print 'The loop is done'

But this one fails to do the same. Can somene help me decipher where exactly I am going wrong.

Thanks for your help

regards
Ram
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,050
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 416
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: string concatenate

  #2  
Apr 11th, 2005
user_name = 'user'
for i in range ( 1, 100 ):
  user = user_name + str ( i )
  print user
else:
  print 'The loop is done'
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,414
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 173
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Solution Re: string concatenate

  #3  
Apr 13th, 2005
If you want to do something even closer to sprintf(), you can use the formatting statement. As Narue pointed out, in Python you must use indentations to group your statements.
[php]
user_name = 'user'
for i in range ( 1, 100 ):
#user = user_name + str ( i )
user = "%s%d" % (user_name, i)
print user
else:
print 'The loop is done'
[/php]
Note that the formatting code %s or %d etc. is the same as used in C for printf() or sprintf(). So if you use "%s%02d", you zero pad the numbers 1 to 9 and get strings like 'user01' and so on.
May 'the Google' be with you!
Reply With Quote  
Join Date: Apr 2005
Location: Bangalore, India
Posts: 6
Reputation: s-sriram is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
s-sriram's Avatar
s-sriram s-sriram is offline Offline
Newbie Poster

Re: string concatenate

  #4  
Apr 13th, 2005
Originally Posted by vegaseat
If you want to do something even closer to sprintf(), you can use the formatting statement. As Narue pointed out, in Python you must use indentations to group your statements.
[php]
user_name = 'user'
for i in range ( 1, 100 ):
#user = user_name + str ( i )
user = "%s%d" % (user_name, i)
print user
else:
print 'The loop is done'
[/php]
Note that the formatting code %s or %d etc. is the same as used in C for printf() or sprintf(). So if you use "%s%02d", you zero pad the numbers 1 to 9 and get strings like 'user01' and so on.

Thanks a lot. That helps. And that is quite close what I expected.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Python Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 3:28 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC