LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-25-2014, 09:53 PM   #1
Zero4
Member
 
Registered: Jun 2007
Posts: 71

Rep: Reputation: 0
Python function-string


Hi
I am learning Python and I am writing an function that takes a list as a parameter and returns a string like so:

List: item1, item2, item3, .......item24

Here is what my code looks like:


Code:
#!/usr/bin/python3

def to_string(alist):
	s=''
	for i in alist:
		s=s+i
		return s

alist=['123','456','789','101']
result=to_string(alist)
print(alist)
This code outputs 123456789101 and i have played with it a lot and got
many different looking strings.
Thanks in advance

Last edited by Zero4; 04-25-2014 at 09:56 PM.
 
Old 04-25-2014, 10:37 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
The code, as it is above, correctly outputs an unmodified version of the list 'alist'.

Can you clarify your question please? Do you want it to output:

1, 2, 3, 4, 5, 6, ....?

If so you could use:
Code:
def to_str(alist):
    s = ''
    for i in alist:
        for x in i:
            s += x
    return ', '.join(s)

alist=['123','456','789','101']
result=to_str(alist)
print(result)

or simply:
Code:
def to_str2(alist):
        return ', '.join(''.join(alist))

alist=['123','456','789','101']
result=to_str2(alist)
print(result)

Last edited by sycamorex; 04-25-2014 at 10:55 PM.
 
Old 04-25-2014, 11:04 PM   #3
Zero4
Member
 
Registered: Jun 2007
Posts: 71

Original Poster
Rep: Reputation: 0
Thanks sycamorex
Yes i do want it to output 1, 2, 3, 4, 5, 6 but without the comma at the end of the last item. And the "List:" at the front.
That's the tricky bit, I can't make work, getting it to output the List at the front and no comma at the end.
 
Old 04-25-2014, 11:10 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by Zero4 View Post
Thanks sycamorex
Yes i do want it to output 1, 2, 3, 4, 5, 6 but without the comma at the end of the last item. And the "List:" at the front.
That's the tricky bit, I can't make work, getting it to output the List at the front and no comma at the end.
Code:
def to_str2(alist):
        return 'List: ' + ', '.join(''.join(alist))

alist=['123','456','789','101']
result=to_str2(alist)
print(result)
Wouldn't that do it?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with simple C string function fahad.anwar Programming 4 07-24-2012 03:40 AM
python --> replace string devilboy09 Programming 4 03-19-2012 03:47 AM
python, os.system() function. howto use python variables? jhwilliams Programming 5 07-28-2007 01:56 AM
can a function return a string? hubabuba Programming 13 03-06-2005 02:51 PM
passing a string to a function jkobrien Programming 8 11-05-2003 01:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration