•
•
•
•
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
![]() |
•
•
Join Date: Apr 2005
Location: Bangalore, India
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
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.
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,414
Reputation:
Rep Power: 9
Solved Threads: 173
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.
[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!
•
•
Join Date: Apr 2005
Location: Bangalore, India
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
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.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
- mysql DELETE not working (PHP)
- Problems with my bus ticket program. (Python)
- Parse error: parse error, unexpected T_STRING (PHP)
- Parse a concatenated variable and string? (PHP)
Other Threads in the Python Forum
- Previous Thread: what is python
- Next Thread: What are the .apy files in Python



Linear Mode