String concatenation produces incorrect output in Python?

Posted by Brian on Stack Overflow See other posts from Stack Overflow or by Brian
Published on 2010-08-19T19:51:16Z Indexed on 2012/07/07 15:16 UTC
Read the original article Hit count: 237

Filed under:
|
|
|

I have this code:

filenames=["file1","FILE2","file3","fiLe4"]


def alignfilenames():
    #build a string that can be used to add labels to the R variables.
    #format goal: suffixes=c(".fileA",".fileB")
    filestring='suffixes=c(".'
    for filename in filenames:
        filestring=filestring+str(filename)+'",".'

    print filestring[:-3]
    #now delete the extra characters
    filestring=filestring[-1:-4]
    filestring=filestring+')'
    print "New String"
    print str(filestring)

alignfilenames()

I'm trying to get the string variable to look like this format: suffixes=c(".fileA",".fileB".....) but adding on the final parenthesis is not working. When I run this code as is, I get:

suffixes=c(".file1",".FILE2",".file3",".fiLe4"
New String
)

Any idea what's going on or how to fix it?

© Stack Overflow or respective owner

Related posts about python

Related posts about string