Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1340346
  • 博文数量: 243
  • 博客积分: 888
  • 博客等级: 准尉
  • 技术积分: 2955
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-05 14:33
个人简介

漫漫长路,其修远兮!

文章分类

全部博文(243)

文章存档

2017年(2)

2016年(22)

2015年(32)

2014年(57)

2013年(107)

2012年(23)

分类: Python/Ruby

2014-10-15 18:52:44

一.字符串连接
1:Python中可以使用'+'、'+='、连接若干个字符串,如果是以下情况,Python则自动连接:
>>> str = 'fish' 'hat' #若干个字符串之间如果只有空格,Python会自动连接
>>> print str
fishhat
>>> str += ' learning Python!' #使用运算符追加字符串
>>> print str
fishhat learning Python
2:使用str2.join(str)函数进行连接,其中str为需要连接的字符串序列或者一个字符串,str2为连接中填补的字符:
>>> string = ('apple','banana','china')
>>> print '-'.join(string)      #向string这个元组中的多个字符串元素之间加上'-'然后输出
apple-banana-china 
>>> print ''.join(string)      #加入的字符也可以是空的
applebananachina
>>> print '-'.join('fishhat')   #直接使用
f-i-s-h-h-a-t                     #自动在每个子字符中加入'-'
3.使用字符串格式
>>> str12 = '%s %s' % (str1, str2)  
>>> print str12  
hello world  


















阅读(1554) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~