python字符串操作(连接、比较、格式化等)(转)
字符串连接
方法一:
Python代码
>>> str1 = 'hello'
>>> str2 = 'world'
>>> str1_2 = str1 + ' ' + str2
>>> str1_2
'hello world'
>>> print str1_2
hello world
>>>
方法二:
Python代码
>>> str12 = '%s %s' % (str1, str2)
>>> print str12
hello world
注意,下面方式是利用str1,str2初始化一个元组:
Python代码
>>> str_12 = str1, str2
>>> str_12
('hello', 'world')
#方括弧才是列表,元组是不可改变滴
>>> str_1_2 = ['hello', 'world']
>>> str_1_2
['hello', 'world']
#另外顺便提一下,print末尾加逗号是把换行符替代成一个空格的意思。
>>> print 'hello',\
... 'world'
hello world
===============================================
字符串比较
Python代码
>>> str = 'info' #我就犯这个错误,因为c中用str做变量很简洁
>>> cmp(str, 'info')
0 >>> str == 'info' #返回值类型不同!!!尽管if语句中都能使用...
True
=================================================
字符串注意事项:
1. 不要像C语言那样使用str作为变量,因为str在python中是一个关键字,用于转换成字符串类型。
Python代码
>>> str = 'hello'
>>> i = 5
>>> str1 = str(i)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
退出python后重新进入(只能退出重新运行,因为这里不是命令行而是python在对stdin进行解释执行(跟读入一个文件执行是一个道理,不过每次都要等待输入一行而已,行缓冲):
Python代码
>>> str1 = 'hello'
>>> i = 5
>>> str2 = str(i)
>>> print str2
5
2. 要理解for x in yyy:中x不是关键词,而是一个我们定义的变量,python每次循环为这个变量赋值yyy中的1个(从最小到最大序号)。
如:
Python代码
>>> str1 = ['str1', 'str2', 'str3']
>>> for each in str1: #这里的each可以是任何变量名
... print each,
...
str1 str2 str3
>>>
python字符串操作(连接、比较、格式化等)(转)的更多相关文章
- 转 Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
转自: http://www.cnblogs.com/huangcong/archive/2011/08/29/2158268.html 黄聪:Python 字符串操作(string替换.删除.截取. ...
- Python 字符串操作
Python 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) 去空格及特殊符号 s.strip() .lstrip() .rstrip(',') 复制字符 ...
- python字符串操作实方法大合集
python字符串操作实方法大合集,包括了几乎所有常用的python字符串操作,如字符串的替换.删除.截取.复制.连接.比较.查找.分割等,需要的朋友可以参考下: #1.去空格及特殊符号 s.st ...
- Python 字符串操作及string模块使用
python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串操作需求: python的字符串属性函数 python的string模块 1.字符串属性方法操作: 1.>字符串格式输出对 ...
- python字符串操作总结
python中有各种字符串操作,一开始python有个专门的string模块,要使用需先import string.后来从python2.0开始,string方法改用str.method()形式调用, ...
- python字符串操作大全
1.去空格 strip() >>> s = 'a b c d ' >>> s.strip() 'a b c d' 2.lstrip() 方法用于截掉字符串左边的空格 ...
- python:字符串的连接
python中有很多字符串连接方式,今天在写代码,顺便总结一下: 最原始的字符串连接方式:str1 + str2 python 新字符串连接语法:str1, str2 奇怪的字符串方式:str1 st ...
- 『无为则无心』Python序列 — 17、Python字符串操作常用API
目录 1.字符串的查找 @1.find()方法 @2.index()方法 @3.rfind()和rindex()方法 @4.count()方法 2.字符串的修改 @1.replace()方法 @2.s ...
- Python字符串操作之字符串分割与组合
12.字符串的分割和组合 12.1 str.split():字符串分割函数 通过指定分隔符对字符串进行切片,并返回分割后的字符串列表. 语法: str.split(s, num)[n] 参数说明: s ...
- python 字符串操作。。
#字符串操作 以0开始,有负下标的使用0第一个元素,-1最后一个元素,-len第一个元 素,len-1最后一个元素 name= "qwe , erw, qwe "print(nam ...
随机推荐
- MVC linq To SQL更新数据库操作
首先在视图中提交数据,使用Html.BeginForm() @using(Html.BeginForm()) { @Html.EditorForModel() //编辑模板.控制器中传过来的数据 &l ...
- Linux-sort用法
本文为转载,原地址:http://www.cnblogs.com/dong008259/archive/2011/12/08/2281214.html sort命令是帮我们依据不同的数据类型进行排序, ...
- WebApi参数传递
c# webapi的参数传递方式:1.查询字符串(query string):2.内容主体(content body) 当然也有cookie或url部分或头部信息(header)等其它传方式,这里仅讨 ...
- 25个CSS3 渐变和动画效果教程
随着最新版CSS3渐变和动画功能发布,Web开发者在开发的过程中有了更多的选择.实际上,已经有了一些替代的技术,目的都是使网站的建设变得简易,高效和快速.不过CSS3所提供的渐变功能有着显著的优点,特 ...
- 好用的shell命令行: fish的配置
fish的可视化配置命令: $ fish_config 其配置文件夹为 ~/.config/fish. 1.要设置环境变量,在配置文件夹里新建 config.fish 文件,它会作为fish 启动时的 ...
- cocos2d-x 2.2.5 安卓工程编译的问题
原址:http://www.cocoachina.com/bbs/read.php?tid=217124 新的cocos2d-x 2.2.5 在使用Eclipse的安卓NDK 9 的编译器进行编译的时 ...
- 2.VS2010C++各种后缀说明
相关资料:"http://blog.csdn.net/kibaamor/article/details/18700607""http://blog.chinaunix.n ...
- Debugging Information in Separate Files
[Debugging Information in Separate Files] gdb allows you to put a program's debugging information in ...
- 代码SketchPaintCode绘制
作者:codeGlider 在我的上一篇文章中 swift10分钟实现炫酷的导航控制器跳转动画,有一个swift logo的形状 上一篇文章的动画 我说的就是中间用来做遮罩的形状. 它不是图片是用一段 ...
- ajax提交后台自动反序列化成对象
下面是ajax提交的方法,主要用到了$form.serializeArray()将表单序列化成json对象,然后用each处理一下,代码如下: var $form = $('<form>& ...