473,378 Members | 1,482 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

List to string


Hi,

I've a list like this..
str1 = ['this is a test string inside list']

I am doing it this way.

for s in str1:
temp_s = s
print temp_s

Any better suggestions?

Thank you,
hj

Mar 18 '07 #1
8 3447
On Mar 18, 12:28 am, "Hitesh" <hitesh...@gmail.comwrote:
Hi,

I've a list like this..
str1 = ['this is a test string inside list']

I am doing it this way.

for s in str1:
temp_s = s
print temp_s

Any better suggestions?

Thank you,
hj
I want to cast value of a list into string..

Mar 18 '07 #2
On Sat, 17 Mar 2007 21:28:56 -0700, Hitesh wrote:
>
Hi,

I've a list like this..
str1 = ['this is a test string inside list']
That's a bad name for the variable. It's called "str1" but it is a list.

I am doing it this way.

for s in str1:
temp_s = s
print temp_s
That's redundant. Why not just "print s"? That will work perfectly.
You can do any of the following, depending on what you are trying to do.
my_list = ["first string", "second string", "third string"]

# print the entire list as a string
print my_list

# print each string individually
for s in my_list:
print s

# save the string representation of the entire list as a variable
my_str = repr(my_list)
another_str = str(my_list)

Note that repr() and str() may have different results, depending on the
type of object you pass into them.
# extract the first string into another variable
my_str = my_list[0]

# insert the third string into another string
my_string = "This is the %s here." % my_list[2]
Hope that helps.
--
Steven.

Mar 18 '07 #3
Hitesh a écrit :
On Mar 18, 12:28 am, "Hitesh" <hitesh...@gmail.comwrote:
>Hi,

I've a list like this..
str1 = ['this is a test string inside list']

I am doing it this way.

for s in str1:
temp_s = s
print temp_s
Why this useless temp_s var ?
>>
Any better suggestions?

Thank you,
hj

I want to cast value of a list into string..
There's no "cast" in Python. It would make no sens in a dynamically
typed language, where type informations belong to the LHS of a binding,
not the RHS.

I guess that what you want is to build a string out of a list of
strings. If so, the answer is (assuming you want a newline between each
element of the list):

print "\n".join(str1)
Mar 19 '07 #4
On Mar 19, 8:11 am, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.comwrote:
Hitesh a écrit :
On Mar 18, 12:28 am, "Hitesh" <hitesh...@gmail.comwrote:
Hi,
I've a list like this..
str1 = ['this is a test string inside list']
I am doing it this way.
for s in str1:
temp_s = s
print temp_s

Why this useless temp_s var ?
Any better suggestions?
Thank you,
hj
I want to cast value of a list into string..

There's no "cast" in Python. It would make no sens in a dynamically
typed language, where type informations belong to the LHS of a binding,
not the RHS.

I guess that what you want is to build a string out of a list of
strings. If so, the answer is (assuming you want a newline between each
element of the list):

print "\n".join(str1)

Thank you guys. Yes that helped. :)

hj
Mar 19 '07 #5
On Mon, 19 Mar 2007 13:11:09 +0100, Bruno Desthuilliers wrote:
There's no "cast" in Python. It would make no sens in a dynamically
typed language, where type informations belong to the LHS of a binding,
not the RHS.
Surely you have left and right mixed up?

x = 1
x = None
x = "spam"
x = []

The name x has no type associated with it. The object bound to the name
does.

--
Steven.

Mar 20 '07 #6
Steven D'Aprano a écrit :
On Mon, 19 Mar 2007 13:11:09 +0100, Bruno Desthuilliers wrote:
>There's no "cast" in Python. It would make no sens in a dynamically
typed language, where type informations belong to the LHS of a binding,
not the RHS.

Surely you have left and right mixed up?
(rereading)
(ashamed)
Obviously, yes.
Thanks for the correction.
Mar 20 '07 #7
On Tue, 20 Mar 2007 13:01:36 +0100, Bruno Desthuilliers wrote:
Steven D'Aprano a écrit :
>On Mon, 19 Mar 2007 13:11:09 +0100, Bruno Desthuilliers wrote:
>>There's no "cast" in Python. It would make no sens in a dynamically
typed language, where type informations belong to the LHS of a binding,
not the RHS.

Surely you have left and right mixed up?

(rereading)
(ashamed)
Obviously, yes.
Thanks for the correction.
That's okay, I have a big "L" and "R" written on the bottom of my shoes.
Of course, they didn't do me any good until I got a "L" and "R" tattooed
on my feet.

--
Steven.

Mar 20 '07 #8
"Steven D'Aprano" <st***@REMOVE.THIS.cyber...ce.com.auwrote:

On Tue, 20 Mar 2007 13:01:36 +0100, Bruno Desthuilliers wrote:

8< ------- confusion about left and right ------------

It gets worse.

When you work on a lathe,
a "right hand cutting tool"
has its cutting edge
on the left...

And the worse part is that
its for good reason.

- Hendrik

Mar 21 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Klaus Neuner | last post by:
Hello, I need a function that converts a list into a set of regexes. Like so: string_list = print string_list2regexes(string_list) This should return something like:
4
by: blrmaani | last post by:
Here is what I want: string s1 = "This is a list of string"; list<string> s2 = s1.some_method(); Now, I should be able to traverse list s2 and get each member ( which is of type 'string' ). ...
13
by: na1paj | last post by:
here's a simple linked list program. the DeleteNode function is producing an infinit loop i think, but i can't figure out where.. #include <stdio.h> typedef struct { char *str; //str is a...
6
by: Henrik Goldman | last post by:
Hello, I have a dataset which consist of a string username and string hostname as a key and then an integer representing a count as the matching "second" value in a pair. So far I've used...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.