; ;

python连接字符串的方式

发现Python连接字符串又是用的不顺手,影响速度

1.数字对字符进行拼接

s=""   #定义这个字符串,方便做连接
print type(s)
for i in range(10):
    print i
    type(i)
    s+=str(i)   #转换类型在对接
print s

 

  2.字符对字符进行拼接

 
string="abcdef"
for i in string:
    print i+'jun'   直接使用字符串连接

3.列表和字符串的拼接

list1=['hello','its','ok']
print 'jun*'.join(list1)  #主要是使用这个join这个函数

4.字典和字符串拼接

这个就必须让字典转为str类型

dict1={'1':'a','2':'b','3':'c'}
str1=str(dict1)
print type(str1)
str1+='jun'
print str1
type(str1)

 

posted @ 2016-11-05 23:11  做梦的人-  阅读(443)  评论(0编辑  收藏  举报