|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
||||
|
||||
|
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 |
|
#4
|
|||
|
|||
|
thanks for that.
it didn't bring up any errors so i didn't realise it was incorrect |
|
#5
|
||||
|
||||
|
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. Last edited by netytan : February 13th, 2004 at 04:10 PM. |
|
#6
|
|||
|
|||
|
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).
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > concatenating strings and numbers |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|