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 ...
随机推荐
- Spring MVC 问题列表:目录
学习SpringMVC时遇到不少问题,这里将其汇总. 1.怎么搭建SpringMVC 2.SpringMVC和Spring使用是配置到一个文件中还是两个配置文件 3.SpringMVC接受从前台请求 ...
- 【转】What's the difference between simulation and emulation
摘要:这2个单词 还是用英文解释,比较准确.按我的理解:simulation就是模拟,可以做些改变. emulation是仿真,是按照原来的样子进行部署,不可以改变. Yes, the concept ...
- JXSE and Equinox Tutorial, Part 2
http://java.dzone.com/articles/jxse-and-equinox-tutorial-part-0 ———————————————————————————————————— ...
- VS2010在C#头文件添加文件注释的方法(转)
步骤: 1.VS2010 中找到(安装盘符以C盘为例)C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplatesCa ...
- UIView的autoresizingMask属性
今天做相册列表的时候,发现有些 UITableViewController 属性不好记忆,然后就查找了一些资料.做一下备份. 在 UIView 中有一个autoresizingMask的属性,它对应的 ...
- Android_UI_点击按钮切换背景效果实现
实现按钮按下和释放,按钮背景图片相应切换效果的方法这里介绍两种,一种是在代码里实现,另一种是在xml文件里实现 一.在xml文件里 首先现在layout的一个xml文件下定义Button如下所示: [ ...
- drop,truncate与delete的区别
注意:这里说的delete是指不带where子句的delete语句 相同点 truncate和不带where子句的delete, 以及drop都会删除表内的数据 不同点: 1. truncate和 d ...
- 使用maphilight高亮显示map的指定area
引用jquery.maphilight.js之后,调用如下方法: //obj参数是代表一个或者多个area的jQuery对象 function areaLight(obj) { var data = ...
- 使用sql访问EXECL文件
--使用sql语句打开访问EXECL文件 --SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDat ...
- android 小知识点
小知识点总结 1. android中MotionEvent.ACTION_CANCEL事件如何被触发? 对于这个问题,android文档的说明很简短,想看明白很难.国外一网页说的还比较详细,写在这里分 ...