Python系列教程(七):字符串

1.1 字符串的格式

在python中,字符串是以单引号'或双引号"括起来的任意文本,比如'abc',"xyz"等等

如下定义的变量b,存储的是字符串类型的值

b = "hello python"
或者
b = 'hello python'

1.2 字符串输出

示例如下:

name = 'joy'
text= '你好'
phone='13344880909'

print('--------------------------------------------------')
print("姓名:%s"%name)
print("职位:%s"%text)

结果:

--------------------------------------------------
姓名: joy
内容: 你好
电话: 13344880909

1.3 下标索引

所谓“下标”,就是编号,就好比超市中的存储柜的编号,通过这个编号就能找到相应的存储空间

列表与元组支持下标索引好理解,字符串实际上就是字符的数组,所以也支持下标索引。

如果想取出部分字符,那么可以通过下标的方法,(注意python中下标从 0 开始)

   name = 'abcdef'

   print(name[0])
   print(name[1])
   print(name[2])

结果:

a
b
c

1.4 字符串常见操作

如有字符串mystr = 'hello world python world hello',以下是常见的操作

<1>find

检测 str 是否包含在 mystr中,如果是返回开始的索引值,否则返回-1

语法:mystr.find(str, start=0, end=len(mystr))

  mystr.find('python')
  mystr.find('python',0,10)

结果:

12
-1

<2>index

跟find()方法一样,只不过如果str不在 mystr中会报一个异常.

**语法:mystr.index(str, start=0, end=len(mystr)) **

  mystr.index('python')
  mystr.index('python',0,10)

结果:

12
File "/usercode/file.py", line 5, in <module>
    mystr.index('python',0,10)
ValueError: substring not found

<3>count

返回 str在start和end之间 在 mystr里面出现的次数

语法:mystr.count(str, start=0, end=len(mystr))

  mystr.count('python')
  mystr.count('l')

结果:

 1
 6

<4>replace

把 mystr 中的 str1 替换成 str2,如果 count 指定,则替换不超过 count 次.

语法:mystr.replace(str1, str2, mystr.count(str1))

  mystr.replace('hello','HELLO')
  mystr.replace('hello','HELLO',1)

结果:

 HELLO worLd python worLd HELLO
 HELLO world python world hello

<5>split

以 str 为分隔符切片 mystr,如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串

**语法:mystr.split(str,maxsplit ) **

  mystr.split(' ')
  mystr.replace(' ',2)

结果:

 ['hello', 'world', 'python', 'world', 'hello']
 ['hello', 'world', 'python world hello']

<6>capitalize

把字符串的第一个字符大写

语法:mystr.capitalize()

  mystr.capitalize()

结果:

 Hello world python world hello

<7>title

把字符串的每个单词首字母大写
语法:mystr.title()

  mystr.title()

结果:

 Hello World Python World Hello

<8>startswith

检查字符串是否是以 obj 开头, 是则返回 True,否则返回 False

语法:mystr.startswith(obj)

  mystr.startswith('hello')
  mystr.startswith('ni')

结果:

  True
  False

<9>endswith

检查字符串是否以obj结束,如果是返回True,否则返回 False.

语法:mystr.endswith(obj)

  mystr.endswith('hello')
  mystr.endswith('Python')

结果:

  True
  False

<10>lower

转换 mystr 中所有大写字符为小写

语法:mystr.lower()

  mystr.lower()

结果:

  hello world python world hello

<11>upper

转换 mystr 中的小写字母为大写

语法:mystr.upper()

  mystr.upper()

结果:

  HELLO WORLD PYTHON WORLD HELLO

<12>ljust

返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串

语法:mystr.ljust(width)

  mystr.ljust(30)

结果:

  'hello world python world hello          '

<13>rjust

返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串

语法:mystr.rjust(width)

  mystr.rjust(40)

结果:

 '          hello world python world hello'

<14>center

返回一个原字符串居中,并使用空格填充至长度 width 的新字符串

语法:mystr.center(width)

  mystr.center(40)

结果:

 '     hello world python world hello     '

<15>lstrip

删除 mystr 左边的空白字符

语法:mystr.lstrip()

a = "  abc "
a.lstrip()

结果

'abc '

<16>rstrip

删除 mystr 字符串末尾的空白字符

a = "  abc "
a.rstrip()

结果

' abc'

语法:mystr.rstrip()

<17>strip

删除mystr字符串两端的空白字符

a = "  abc "
a.strip()

结果

'abc'

<18>rfind

类似于 find()函数,不过是从右边开始查找.

语法:mystr.rfind(str, start=0,end=len(mystr) )

  mystr.rfind('python')
  mystr.rfind('python',0,10)

结果:

12
-1

<19>rindex

类似于 index(),不过是从右边开始.

语法:mystr.rindex( str, start=0,end=len(mystr))

  mystr.rindex('python')
  mystr.rindex('python',0,10)

结果:

12
报错:ValueError: substring not found

<20>partition

把mystr以str分割成三部分,str前,str和str后

语法:mystr.partition(str)

  mystr.partition('world')

结果:

 ('hello ', 'world', ' python world hello')

<21>rpartition

类似于 partition()函数,不过是从右边开始.

语法:mystr.rpartition(str)

  mystr.rpartition('world')

结果:

 ('hello world python ', 'world', ' hello')

<22>splitlines

按照行分隔,返回一个包含各行作为元素的列表

语法:mystr.splitlines()

  mystr.splitlines()

结果:

['hello world python world hello']

<23>isalpha

如果 mystr 所有字符都是字母 则返回 True,否则返回 False

语法:mystr.isalpha()

  mystr.isalpha()

结果:

False

<24>isdigit

如果 mystr 只包含数字则返回 True 否则返回 False.

**语法:mystr.isdigit() **

  mystr.isdigit()

结果:

False

<25>isalnum

如果 mystr 所有字符都是字母或数字则返回 True,否则返回 False

语法:mystr.isalnum()

  mystr.isalnum()

结果:

False

<26>isspace

如果 mystr 中只包含空格,则返回 True,否则返回 False.

语法:mystr.isspace()

  mystr.isspace()

结果:

False

<27>join

mystr 中每个字符后面插入str,构造出一个新的字符串

语法:mystr.join(str)

  '-'.join(mystr)

结果:

h-e-l-l-o- -w-o-r-l-d- -p-y-t-h-o-n- -w-o-r-l-d- -h-e-l-l-o
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,036评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,046评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,411评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,622评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,661评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,521评论 1 304
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,288评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,200评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,644评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,837评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,953评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,673评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,281评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,889评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,011评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,119评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,901评论 2 355