首页 > 编程语言 > 详细

Python基础(2) 字符串

时间:2020-02-07 02:00:42      阅读:133      评论:0      收藏:0      [点我收藏+]

1 单引号、双引号和三引号

  字符串是一系列任意文本,Python中的字符串通常用‘文本‘或"文本"表示,"""文本"""除了空格还可以保留换行符,同时也可以使用反斜杠(\)转义特殊字符。

 

a = hello world!
b = "Can I help you!"
c = "I‘m OK!"   # 此处用""保留字符中的‘
d = This is a "sun" day.  # 此处用‘‘保留字符中的""
e = """Good     # 三个引号除了空格,还可保存换行
morning"""
print(a, b, c, d, e)

 

  输出:

hello world! Can I help you! Im OK! This is a "sun" day. Good
morning

2 字符串操作

  (1+:连接字符串

  (2*:复制字符串

  (3[start:end:step]:截取字符串,三个值可省略可为负数

a = Can I help you!
b = Tom, 
print(a[3: 10])     # I help 字符串截取:变量[start:end] 可为负数
print(a[2:10:2])    # nIhl 变量[start:end:step]
print(b + a)    # Tom, Can I help you!  (+)是字符串的连接符
print(b * 5)    # Tom, Tom, Tom, Tom, Tom, (*)表示复制当前字符串

3 转义符

转义字符

描述

\(在行尾时)

续行符

\\

反斜杠

\’

单引号

\’’

双引号

\a

响铃

\b

退格

\e

转义

\000

\n

换行

\v

纵向制表符

\t

横向制表符

\r

回车

\f

换页

\oyy

八进制数yy代表的字符,例如:\o12代表换行

\xyy

十进制数yy代表的字符,例如:\x0a代表换行

\other

其他的字符以普通格式输出

  如果不想让转义字符生效,可以用空rR定义原始字符串:print(r‘\t\r‘)      # \t\r

  注意:如果你想用母语写文本,就需要一个支持unicode的编辑器,因为unicode时书写国际文本的标准方法,Python允许处理unicode文本,只要在字符串前加前缀uU。例如:u”This ia a Unicode string.”

4 格式化输出

  常用格式符号:%[(name)][flags][width].[precision]typecode

name = Tom
age = 22
print(大家好,我是 %s, 我今年%d岁了,很高兴认识你们。 % (name, age))

 

类型码

含义

diu

十进制

o

八进制

X/x

十六进制

E/e

科学记数法表示的浮点数

F/f

十进制浮点数

G/g

按照e或者f格式返回

c

接受整数或者单个ascii字符

r

返回repr形式转换python对象

s

str1形式返回python对象

5 常用函数

  种类和数量比较多,平时需要多练习积累。

 

str1 = China is my motherland!
print(str1.find(mo))  # 12 检测字符串是否包含指定字符,是则返回索引值,否返回-1
print(str1.index(m))  # 9 检测字符串是否包含指定字符,是则返回索引值,否返回错误
print(str1.count(h, 1, len(str1)))    # 2 返回str在string中指定范围内[start, end)出现的次数
print(str1.replace(China, Tailand))     # Tailand is my motherland! 替换字符,可指定次数 

str2 = hello world hello china
print(str2.split( , 2))      # [‘hello‘, ‘world‘, ‘hello‘, ‘china‘] 分割字符串,可定义个数
print(str2.partition(world))   # (‘hello ‘, ‘world‘, ‘ hello china‘) 将字符串以str分割成三个部分(前、本身、后)
print(str2.capitalize())    # Hello world hello china 首字母大写
print(str2.title())         # Hello World Hello China 每个单词首字母大写
print(str2.startswith(hello))     # True 检查开头
print(str2.endswith(china))       # True 检查结尾 

str3 = Hello World HELLO CHINA
print(str3.lower())     # hello world hello china   全部小写
print(str3.upper())     # HELLO WORLD HELLO CHINA   全部大写
print(str3.ljust(30))   # Hello World HELLO CHINA   左对齐
print(str3.rjust(30))   #        Hello World HELLO CHINA    右对齐
print(str3.center(30))  #    Hello World HELLO CHINA        居中

str4 =    hello   
print(str4.lstrip())    # hello     去除左边空格
print(str4.rstrip())    #    hello  去除右边空格
print(str4.strip())     # hello 去除两边空格 

str5 = -
str6 = a123
print(str5.join(str6))  # a-1-2-3   每个字符以特定符号连接
print(str5.isspace())   # False 是否只包含空格
print(str6.isalnum())   # True  是否只包含字母和数字
print(str6.isalpha())   # False 是否只包含字母
print(str6.isdigit())   # False 是否只包含数字

 

Python基础(2) 字符串

原文:https://www.cnblogs.com/wisefaith/p/12270971.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!