Search code examples
pythonstringmethodsstring-conversion

How to use str.join()?


>>>seq = ("a", "b", "c") # This is sequence of strings.
str.join( "-",seq )
SyntaxError: multiple statements found while compiling a single statement

What went wrong here? I tried to change " by ' but it doesn't help...

>>> seq = ("a", "b", "c") 
my_string = "-"
str.join(my_string, seq)
SyntaxError: multiple statements found while compiling a single statement

why?????


Solution

  • It can be done even easier, as the object on which is method called is passed as the first argument, so writing

    seq = ("a", "b", "c") 
    my_string = '-'
    print my_string.join(seq)
    

    gives

    'a-b-c'
    

    as expected