python输出列表不带中括号和引号

转自:https://blog.csdn.net/u013656369/article/details/80464544

平时python输出list字符串时,会自动加上引号和中括号。

比如

 
  1. str=['hello','world']

  2. >>> str

  3. ['hello', 'world']

方法1

可以用join方法:

 
  1. >>> print " ".join(str)

  2. hello world

其中:Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串。

比如:

 
  1. str = "-"

  2. seq = ("a", "b", "c")

  3. print str.join( seq )

输出结果:

a-b-c

方法2:如果list存放的是数字,则:将其转为int即可。因为原来存放的是string型的

 
  1. str=['1', '2', '3']

  2. for i in str:

  3.     int(i)

  4. print(i)

  5.  

猜你喜欢

转载自blog.csdn.net/orangefly0214/article/details/81289048