Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old February 13th, 2004, 08:15 AM
mattcronin57 mattcronin57 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 12 mattcronin57 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
concatenating strings and numbers

hi

i want to create five fies with sequentially numbered nameseg

filebackup_1.tar.gz,
filebackup_2.tar.gz,
etc.

i thought i would be able to do it like this, but python won't allow strings and numbers to be concatenated:

count = 0
for path in paths:
++count
fileName = "filebackup_" + count + ".tar.gz"

i have tried using the following line but can't get that to work either (i can't get the syntax right if it is the right way to do it):

count = string.atoi(count), `count`

please help

Reply With Quote
  #2  
Old February 13th, 2004, 08:25 AM
mattcronin57 mattcronin57 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 12 mattcronin57 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
just found the answer to half of it out

i just found the way to turn the number into a string from another thread (should have looked harder first).

however, the count variable isn't increasing with iteration of the loop

Reply With Quote
  #3  
Old February 13th, 2004, 08:47 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
python is an "easy" transition from C but that does not mean it uses all the same syntax or methods
Code:
for path in paths:
    count +=1
    fileName = "filebackup_" + `count` + ".tar.gz"

should sort you out
Grim

Reply With Quote
  #4  
Old February 13th, 2004, 09:00 AM
mattcronin57 mattcronin57 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 12 mattcronin57 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thanks for that.

it didn't bring up any errors so i didn't realise it was incorrect

Reply With Quote
  #5  
Old February 13th, 2004, 04:06 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Probably the most comon way to do this kind of thing (besides type casting isng str()) is to use string formatting. Which is my persional favourate (depending on the situation) because it gives you a lot of controle and often takes up less space!

Code:
>>> print 'string %d number' % 1
string 1 number


For more info on string formatting you should read this:

http://www.python.org/doc/2.3.3/lib...eq-strings.html

so you're code might end up looking something like this...

Code:
for path in paths:
    count += count
    fileName = 'filebackup_%d.tar.gz' % count


If you dont care about spaces creeping into you're strings (which you do here), you can use ',' to concate strings, number etc, i.e.

Code:
>>> print 'string', 1, 'number'
string 1 number
>>> print 'string', [1, 2, 3, 4, 5], 'list'
string [1, 2, 3, 4, 5] list
>>> print 'string', {'key1': 'value', 'key2': 'value'}, 'dictionary'
string {'key2': 'value', 'key1': 'value'} dictionary
>>> print 'string', (1, 2, 3, 4, 5), 'tuple'
string (1, 2, 3, 4, 5) tuple
>>> 


You get the point i'm sure , this does pretty much the same thing as 'string' + `number` + 'string' format when used with print . Just thought i'd mention it!

Mark.
__________________
programming language development: www.netytan.com Hula


Last edited by netytan : February 13th, 2004 at 04:10 PM.

Reply With Quote
  #6  
Old February 13th, 2004, 04:37 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
You shouldn't use `number`, generally. It's not the same as creating a string from the variable value, it's the same as repr()'ing it. String formatting is the most efficient way of doing it (as opposed to using + to concatenate).
__________________
Debian - because life's too short for worrying.
Best. (Python.) IRC bot. ever.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > concatenating strings and numbers


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway