字符串连接
方法一:

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字符串操作(连接、比较、格式化等)(转)的更多相关文章

  1. 转 Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)

    转自: http://www.cnblogs.com/huangcong/archive/2011/08/29/2158268.html 黄聪:Python 字符串操作(string替换.删除.截取. ...

  2. Python 字符串操作

    Python 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) 去空格及特殊符号 s.strip() .lstrip() .rstrip(',') 复制字符 ...

  3. python字符串操作实方法大合集

    python字符串操作实方法大合集,包括了几乎所有常用的python字符串操作,如字符串的替换.删除.截取.复制.连接.比较.查找.分割等,需要的朋友可以参考下:   #1.去空格及特殊符号 s.st ...

  4. Python 字符串操作及string模块使用

    python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串操作需求: python的字符串属性函数 python的string模块 1.字符串属性方法操作: 1.>字符串格式输出对 ...

  5. python字符串操作总结

    python中有各种字符串操作,一开始python有个专门的string模块,要使用需先import string.后来从python2.0开始,string方法改用str.method()形式调用, ...

  6. python字符串操作大全

    1.去空格 strip() >>> s = 'a b c d ' >>> s.strip() 'a b c d' 2.lstrip() 方法用于截掉字符串左边的空格 ...

  7. python:字符串的连接

    python中有很多字符串连接方式,今天在写代码,顺便总结一下: 最原始的字符串连接方式:str1 + str2 python 新字符串连接语法:str1, str2 奇怪的字符串方式:str1 st ...

  8. 『无为则无心』Python序列 — 17、Python字符串操作常用API

    目录 1.字符串的查找 @1.find()方法 @2.index()方法 @3.rfind()和rindex()方法 @4.count()方法 2.字符串的修改 @1.replace()方法 @2.s ...

  9. Python字符串操作之字符串分割与组合

    12.字符串的分割和组合 12.1 str.split():字符串分割函数 通过指定分隔符对字符串进行切片,并返回分割后的字符串列表. 语法: str.split(s, num)[n] 参数说明: s ...

  10. python 字符串操作。。

    #字符串操作 以0开始,有负下标的使用0第一个元素,-1最后一个元素,-len第一个元 素,len-1最后一个元素 name= "qwe , erw, qwe "print(nam ...

随机推荐

  1. bzoj 1492 [NOI2007]货币兑换Cash(斜率dp+cdq分治)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1492   [题意] 有AB两种货币,每天可以可以付IPi元,买到A券和B券,且A:B= ...

  2. tcpdump dns包(linux高性能编程读书笔记2)

      tcpdump -i eth0 -nt -s 500 port domain host -t A www.baidu.com www.baidu.com is an alias for www.a ...

  3. manacher算法_求最长回文子串长度

    很好的总结,转自: http://blog.csdn.net/dyx404514/article/details/42061017 总结为:两大情况,三小情况. 两大情况:I. i <= p 1 ...

  4. Unable to find vcvarsall.bat解决方法

    今天在安装scikit-learn时出现了 error: Unable to find vcvarsall.bat 在安装一些Python模块时,大部分是cpython写的模块时会发生如下错误 err ...

  5. Linux下U盘的挂载和文件的拷贝

    把文件通过U盘拷贝到linux系统下插好U盘后,查看磁盘情况fdisk -l正常情况下有 Disk /dev/sda:2045 MB,2045247488 bytes47 heads,46 secto ...

  6. php和.net 的加密解密

    PHP版: $key = 335ff'; /* * 加密方法 * @param string $input,待加密的字符串 * @param string $key,加密的密码(只能为8位长) * @ ...

  7. Blog 入职新公司的一些吐槽!

    入职公司已经两个星期了,说真的也很惭愧.我们这小批入职的一共六个人,五个人是实习生,我是唯一一个社招. 所以 我要吐槽 !! 吐槽1 人家都是90后(TAT) 其实真的不要觉得年龄是压力!看看路边KF ...

  8. Python程序的混淆和加密

    混淆 为了增加代码阅读的难度, 源代码的混淆非常必要, 一个在线的Python代码混淆网站. 如果你觉得有用, 可以购买离线版本.同时需要注意的是, 这个混淆其实还是被很多人怀疑的, 因为即使混淆了, ...

  9. ubuntu 14.04 root破解

    Advanced Programmable interrupt controller 高级可编程中断控制;Advanced create table `users` (`id` int(11) not ...

  10. this compilation unit is not on the build of a java project

    this compilation unit is not on the build of a java project java里输入代码就出这个提示,无法写代码了. 找到觉得路径打开文件编辑就好了, ...