python 字符串(2)

简介: 字符串复制字符串复制的方法:>>> a = "I like python and can teach you to learn it.">>> print aI like python and can teach you to learn it.>>> b = a>>> print bI like python and can teach you to learn it.>>> print aI like python and can teach you to learn it.复制非常简单,类似与赋值一样。

字符串复制
字符串复制的方法:

>>> a = "I like python and can teach you to learn it."
>>> print a
I like python and can teach you to learn it.
>>> b = a
>>> print b
I like python and can teach you to learn it.
>>> print a
I like python and can teach you to learn it.

复制非常简单,类似与赋值一样。可以理解为那个字符串本来跟a连接着,通过b=a,a从自己手里分处一股绳子给了b,这样两者都可以指向那个字符串了。

字符串长度

方法:

>>> a="hello"
>>> len(a)
5

使用的是一个函数len(object)。得到的结果就是该字符串长度。

>>> m = len(a)  #把结果返回后赋值给一个变量
>>> m
5
>>> type(m)     #这个返回值(变量)是一个整数型
<type 'int'>

字符大小写的转换

在python中有下面一堆内建函数,用来实现各种类型的大小写转化

  • S.upper() #S中的字母大写
  • S.lower() #S中的字母小写
  • S.capitalize() #首字母大写
  • S.istitle() #检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。
  • S.isupper() #S中的字母是否全是大写
  • S.islower() #S中的字母是否全是小写
    看例子:
>>> a = "hiekay,python"
>>> a.upper()       #将小写字母完全变成大写字母
'HIEKAY,PYTHON'
>>> a               #原数据对象并没有改变
'hiekay,python'
>>> b = a.upper()
>>> b
'HIEKAY,PYTHON'
>>> c = b.lower()   #将所有的小写字母变成大写字母
>>> c
'hiekay,python'

>>> a
'hiekay,python'
>>> a.capitalize()  #把字符串的第一个字母变成大写
'Hiekay,python'
>>> a               #原数据对象没有改变
'hiekay,python'
>>> b = a.capitalize() #新建立了一个
>>> b
'Hiekay,python'

>>> a = "hiekay,github"    #这里的问题就是网友白羽毛指出的,非常感谢他。
>>> a.istitle()
False
>>> a = "HIEAKY"        #当全是大写的时候,返回False
>>> a.istitle()
False
>>> a = "hiekaY"
>>> a.istitle()
False
>>> a = "Hiekay,github"  #如果这样,也返回False
>>> a.istitle()
False
>>> a = "Hiekay"        #这样是True
>>> a.istitle()
True
>>> a = 'Hiekay,Github' #这样也是True
>>> a.istitle()
True

>>> a = "Hiekay"
>>> a.isupper()
False
>>> a.upper().isupper()
True
>>> a.islower()
False
>>> a.lower().islower()
True
>>> a = "This is a Book"
>>> a.istitle()
False
>>> b = a.title()     #这样就把所有单词的第一个字母转化为大写
>>> b
'This Is A Book'
>>> a.istitle()       #判断每个单词的第一个字母是否为大写
False 
>>> b.istitle()
True

操作字符串中的字符

在python中按照这样的顺序对字符串进行编号:从左边第一个开始是0号,向下依次按照整数增加,为1、2...,直到最后一个,在这个过程中,所有字符,包括空格,都进行变好。例如:
Hello,wor ld
对于这个字符串,从左向右的变好依次是:
|0|1|2|3|4|5|6|7|8|9|10|11|
|H|e|l|l|o|,|w|o|r| |l |d |
看代码:

>>> a = "Hello,wor ld"
>>> len(a)      #字符串的长度是12,说明公有12个字符,最后一个字符编号是11
12
>>> a[0]
'H'
>>> a[3]
'l'
>>> a[9]
' '
>>> a[11]
'd'
>>> a[5]
','

特别说明,编号是从左边开始,第一个是0。
能不能从右边开始编号呢?可以。这么人见人爱的python难道这点小要求都不满足吗?

>>> a[-1]
'd'
>>> a[11]
'd'
>>> a[-12]
'H'
>>> a[-3]
' '

看到了吗?如果从右边开始,第一个编号是-1,这样就跟从左边区分开了。也就是a[-1]和a[11]是指向同一个字符。

字符串截取

比如,从“hello,wor ld”里面取出“llo”。可以这样操作

>>> a[2:5]
'llo'
  • 注意: 截取a[n,m],其中n<m,得到的字符是从a[n]开始到a[m-1]
    注意:所截取部分的第一个字符(l)对应的编号是(2),从这里开始;结束的字符是(o),对应编号是(4),但是结束的编号要增加1,不能是4,而是5.这样截取到的就是上面所要求的了。
特殊的
>>> a[:]    #表示截取全部
'Hello,wor ld'
>>> a[3:]   #表示从a[3]开始,一直到字符串的最后
'lo,wor ld'
>>> a[:4]   #表示从字符串开头一直到a[4]前结束
'Hell'

去掉字符串两头的空格

  • S.strip() 去掉字符串的左右空格
  • S.lstrip() 去掉字符串的左边空格
  • S.rstrip() 去掉字符串的右边空格
    例子:
>>> b=" hello "
>>> b
' hello '
>>> b.strip()
'hello'
>>> b
' hello '
>>> b.lstrip()
'hello '
>>> b.rstrip()
' hello'

扩展raw_input()

下面共同做一个练习:输入用户名,计算机自动向这个用户打招呼。代码如下:

#coding:utf-8

print "please write your name:"
name=raw_input()
print "Hello,%s"%name

raw_input()的含义,就是要用户输入内容,所输入的内容是一个字符串。

目录
相关文章
|
1天前
|
Python
【Python操作基础】——字符串
【Python操作基础】——字符串
|
1天前
|
Python
Python字符串和字节不要混淆str.format()和bytes.format()
【5月更文挑战第6天】Python字符串和字节不要混淆str.format()和bytes.format()
4 1
|
1天前
|
Python
Python字符串和字节使用正确的编码/解码
【5月更文挑战第6天】Python字符串和字节使用正确的编码/解码
6 2
|
1天前
|
存储 Python
python字符串和字节明确数据类型
【5月更文挑战第6天】python字符串和字节明确数据类型
6 2
|
2天前
|
Python
Python避免在字符串和字节之间混淆
【5月更文挑战第5天】Python避免在字符串和字节之间混淆
13 3
|
4天前
|
数据安全/隐私保护 开发者 Python
【Python 基础】检查字符串是否只包含数字和字母?
【5月更文挑战第8天】【Python 基础】检查字符串是否只包含数字和字母?
|
4天前
|
Python
【Python 基础】如何将一个字符串转化为全大写和全小写?
【5月更文挑战第8天】【Python 基础】如何将一个字符串转化为全大写和全小写?
|
4天前
|
机器学习/深度学习 存储 人工智能
python 字符串的三种定义方式
python 字符串的三种定义方式
10 1
|
6天前
|
Python Perl
Python中的字符串分析:判断字符串中是否包含字母
Python中的字符串分析:判断字符串中是否包含字母
10 0
|
6天前
|
C语言 Python
【Python 基础】如何进行字符串插值?
【5月更文挑战第6天】【Python 基础】如何进行字符串插值?