[Tutor] Converting a list to a string

Glen Wheeler wheelege@tsn.cc
Mon, 7 May 2001 20:59:15 +1000


This is a multi-part message in MIME format.

------=_NextPart_000_0202_01C0D738.935167A0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable


  I think you want to have a look at string.join().

  For example...

>>> import string
>>> x =3D ['I', 'am', 'a', 'string']
>>> string.join(x)
'I am a string'

  You can also specify the separating character as well, with a second =
argument.  Like...

>>> x =3D ['W', 'o', 'r', 'd']
>>> string.join(x, '')
'Word'

  Hope that helps,
  Glen.

  ----- Original Message -----=20
  From: Praveen Pathiyil=20
  To: tutor@python.org=20
  Sent: Monday, May 07, 2001 8:53 PM
  Subject: [Tutor] Converting a list to a string


  Hi all,

  If i have a list=20
  status =3D ['tftp>', 'Sent', '1943', 'bytes', 'in', '0.0', 'seconds'],
  is there a single command which will give me a string=20
  tftp> Sent 1943 bytes in 0.0 seconds

  OR do i have to do=20

  stat_str =3D ''
  for elt in status:
      stat_str =3D stat_str + ' ' + elt

  TIA,
  Praveen.

------=_NextPart_000_0202_01C0D738.935167A0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 5.50.4522.1800" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV>&nbsp;</DIV>
<DIV>&nbsp; I think you want to have a look at string.join().</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp; For example...</DIV>
<DIV>&nbsp;</DIV>
<DIV>&gt;&gt;&gt; import string</DIV>
<DIV>&gt;&gt;&gt; x =3D ['I', 'am', 'a', 'string']<BR>&gt;&gt;&gt;=20
string.join(x)<BR>'I am a string'</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp; You can also specify the separating character as well, with =
a second=20
argument.&nbsp; Like...</DIV>
<DIV>&nbsp;</DIV>
<DIV>&gt;&gt;&gt; x =3D ['W', 'o', 'r', 'd']<BR>&gt;&gt;&gt; =
string.join(x,=20
'')<BR>'Word'</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp; Hope that helps,</DIV>
<DIV>&nbsp; Glen.</DIV>
<DIV>&nbsp;</DIV>
<BLOCKQUOTE dir=3Dltr=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
  <DIV style=3D"FONT: 10pt arial">----- Original Message ----- </DIV>
  <DIV=20
  style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: =
black"><B>From:</B>=20
  <A title=3Dppathiyi@cisco.com =
href=3D"mailto:ppathiyi@cisco.com">Praveen=20
  Pathiyil</A> </DIV>
  <DIV style=3D"FONT: 10pt arial"><B>To:</B> <A title=3Dtutor@python.org =

  href=3D"mailto:tutor@python.org">tutor@python.org</A> </DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Sent:</B> Monday, May 07, 2001 8:53 =
PM</DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Subject:</B> [Tutor] Converting a =
list to a=20
  string</DIV>
  <DIV><BR></DIV>
  <DIV><FONT face=3DArial size=3D2>Hi all,</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
  <DIV><FONT face=3DArial size=3D2>If i have a list </FONT><FONT =
face=3DArial=20
  size=3D2></FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>status =3D ['tftp&gt;', 'Sent', =
'1943', 'bytes',=20
  'in', '0.0', 'seconds'],</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>is there a single command which will =
give me a=20
  string </FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>tftp&gt; Sent 1943 bytes in 0.0=20
  seconds</FONT></DIV>
  <DIV>&nbsp;</DIV>
  <DIV><FONT face=3DArial size=3D2>OR <FONT face=3DArial size=3D2>do i =
have to do=20
  </FONT></FONT></DIV>
  <DIV>&nbsp;</DIV>
  <DIV><FONT face=3DArial size=3D2>stat_str =3D ''</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>for elt in status:</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; <FONT face=3DArial =

  size=3D2>stat_str =3D <FONT face=3DArial size=3D2>stat_str + ' ' +=20
  elt</FONT></FONT></FONT></DIV>
  <DIV>&nbsp;</DIV>
  <DIV><FONT face=3DArial size=3D2>TIA,</FONT></DIV>
  <DIV><FONT face=3DArial =
size=3D2>Praveen.</FONT></DIV></BLOCKQUOTE></BODY></HTML>

------=_NextPart_000_0202_01C0D738.935167A0--