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

1.数字对字符进行拼接

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

 

python连接字符串的方式

  2.字符对字符进行拼接

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

python连接字符串的方式

3.列表和字符串的拼接

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

python连接字符串的方式

4.字典和字符串拼接

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

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

 python连接字符串的方式

相关文章:

  • 2021-08-27
  • 2022-12-23
  • 2022-12-23
  • 2021-08-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-31
  • 2022-12-23
  • 2021-07-20
相关资源
相似解决方案