小白自学笔记之Python入门-第四章 常用模块-string
4.4 string
Python 没有字符数据类型,单个字符就是长度为 1 的字符串。string模块主要包含关于字符串的处理函数。Python 中的字符串字面量由单引号或双引号括起,'hello' 等同于 "hello",使用三引号"""或者'''可以将多行字符串赋值给变量。不要担心一对单三引号之间是注释,因为这里是赋值给变量哦!就像下面:
########################################
a="""1975年二、三月间,一个平平常常的日子,
细蒙蒙的雨丝夹着一星半点的雪花,正纷纷淋淋地向大地飘洒着。
时令已快到惊蛰,雪当然再不会存留,往往还没等落地,就已经消失得无踪无影了。
黄土高原严寒而漫长的冬天看来就要过去,但那真正温暖的春天还远远地没有到来。"""
b='''Finally , after walking about a hundred miles , we reached the gym . It’s this giant room with a basketball hoop at each end .
“Miss Small?” called Miss Daisy . “Are you here?”
Nobody answered , but there was an echo in the gym so we could hear Miss Daisy’s words over and over again when they bounced off the walls .
“Miss Small? . . . Miss Small? . . . Miss Small? . . . Miss Small? . . . Are you here?. . . Are you here? . . . Are you here?”
It was cool .
“Hello!” I yelled .
The gym yelled back , “Hello! . . . Hello!. . . Hello! . . . Hello! . . . Hello!”
“Echo!” yelled Michael .
“Echo . . . echo . . . echo . . . echo , ”yelled the gym .
“A.J. is stupid!” yelled Ryan .
“A.J. is stupid! . . . A.J. is stupid! . . . A.J.
is stupid! . . . A.J. is stupid!” yelled the gym .
I was gonna yell , “Ryan is a dumb-head , ” but instead Miss Daisy yelled ,“Stop that , boys!”
“Stop that , boys! . . . Stop that , boys! . . .Stop that , boys!” yelled the gym .
It was cool .
'''
print(a)
print(b)
########################################
【上述文字资料汉语部分摘自《平凡的世界》开头,英文部分摘自纽约畅销书best seller Dan Gutman 的my weird school 系列(主要讲的是美国低年级小学生的学校生活)之Miss small is off the wall ,by the way,我真的喜欢看】

Python 有一组可以在字符串上使用的内建方法。而且所有字符串方法都返回新值,不会更改原始字符串,下表中列出了一些方法。



我不想写了,太多了,有点印象就可以了,以后用到再查吧,下面看看几个例子就ok了。
a = "Hello, 中国石油大学(北京)"
print(len(a)) #17
print(a.replace("Hello", "你好")) #你好, 中国石油大学(北京)
s=("信息学院","CISE")
print('**'.join(s)) #信息学院**CISE
name = "魏淑芬"
gender = "女"
age = 29
note = "至今未婚"
introduction = "俺叫{1},{0},今年{2}岁,{3}。"
print(introduction.format(gender, name, age,note))#俺叫魏淑芬,女,29岁,至今未婚
运行结果如下:
17
你好, 中国石油大学(北京)
信息学院**CISE
俺叫魏淑芬,女,今年29岁,至今未婚。