Join the string from array forms

A forum for general discussion of the Python programming language.

Join the string from array forms

Postby zahrein » Thu May 23, 2013 12:49 am

I have a problem on joining the string from array forms. I tried using the join function. seems unable to work.
This is the code that i am trying to perform: ''.join(the_words).
seems not working. Any solutions?

Code: Select all
# Reversing the  string

a='zacko wacko'
the_words =[]
the_words.extend(a)
print the_words

size = len(the_words)
print size

for i in range(0,size/2):

    the_words[i],the_words[size-1-i]=the_words[size-1-i],the_words[i] #algorithms to to swap the words.

print the_words
''.join(the_words)
zahrein
 
Posts: 15
Joined: Wed Apr 03, 2013 1:45 am

Re: Join the string from array forms

Postby metulburr » Thu May 23, 2013 12:52 am

you didnt save it to a variable or do anything with it
Code: Select all
joined = ''.join(the_words)
print(joined)


Code: Select all
a='zacko wacko'
the_words =[]
the_words.extend(a)
print(the_words)

can also be aquired with:
Code: Select all
print(list(a))


Code: Select all
for i in range(0,size/2):

could also be used to index the actual string
Code: Select all
for i in a:


and last but not least, this makes you whole code moot, as this one line also reverses the string
Code: Select all
a='zacko wacko'
print(a[::-1])
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1471
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Join the string from array forms

Postby zahrein » Thu May 23, 2013 2:05 am

Thanks for the suggestions. I will try. I just learn python for the past 2 months. It encourages me to learn more.
zahrein
 
Posts: 15
Joined: Wed Apr 03, 2013 1:45 am


Return to General Discussions

Who is online

Users browsing this forum: Bing [Bot] and 3 guests