#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    12
    Rep 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
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    12
    Rep 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
  4. #3
  5. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    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
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    12
    Rep Power
    0
    thanks for that.

    it didn't bring up any errors so i didn't realise it was incorrect
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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/...q-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 05:10 PM.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    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.

IMN logo majestic logo threadwatch logo seochat tools logo