[White] Science Python Python basic data type (2)

And operating string type

- 字符串类型的表示 
- 字符串操作符 
- 字符串处理函数 
- 字符串处理方法 
- 字符串类型的格式化

String

由0个或多个字符组成的有序字符序列
- 字符串由一对单引号或一对双引号表示 
"请输入带有符号的温度值: " 或者 'C'
- 字符串是字符的有序序列,可以对其中的字符进行索引 
"请" 是 "请输入带有符号的温度值: " 的第0个字符

字符串有 2类共4种 表示方法
- 由一对单引号或双引号表示,仅表示单行字符串 
"请输入带有符号的温度值: " 或者 'C'
- 由一对三单引号或三双引号表示,可表示多行字符串 
''' Python
						语言 '''

- 如果希望在字符串中包含双引号或单引号呢? 
'这里有个双引号(")' 或者 "这里有个单引号(')"
- 如果希望在字符串中既包括单引号又包括双引号呢? 
''' 这里既有单引号(')又有双引号 (") '''

Character string number
Here Insert Picture Description

字符串的使用 
使用[ ]获取字符串中一个或多个字符
- 索引:返回字符串中单个字符 <字符串>[M] 
"请输入带有符号的温度值: "[0] 或者 TempStr[-1]
- 切片:返回字符串中一段字符子串 <字符串>[M: N] 
"请输入带有符号的温度值: "[1:3] 或者 TempStr[0:-1]

字符串切片高级用法 
使用[M: N: K]根据步长对字符串切片
- <字符串>[M: N],M缺失表示至开头,N缺失表示至结尾 
"〇一二三四五六七八九十"[:3] 结果是 "〇一二"
- <字符串>[M: N: K],根据步长K对字符串切片 
"〇一二三四五六七八九十"[1:8:2] 结果是 "一三五七" "〇一二三四五六七八九十"[::-1] 结果是 "十九八七六五四三二一〇"

字符串的特殊字符 
转义符 \
- 转义符表达特定字符的本意 
"这里有个双引号(\")" 结果为 这里有个双引号(")
- 转义符形成一些组合,表达一些不可打印的含义 
"\b"回退 "\n"换行(光标移动到下行首) "\r" 回车(光标移动到本行首)

String operator

An ordered sequence of characters of zero or more characters

Bold Style
Get String week
- Input: an integer of 1-7, showing week
- Output: integer corresponding to the input character string week
- for example: input 3, output Wednesday
Here Insert Picture Description
Here Insert Picture Description

String handling functions
some string processing function provided in the form of
Here Insert Picture Description
Here Insert Picture Description
Unicode encoding

Python字符串的编码方式
- 统一字符编码,即覆盖几乎所有字符的编码方式 
- 从0到1114111 (0x10FFFF)空间,每个编码对应一个字符 
- Python字符串中每个字符都是Unicode编码字符

Here Insert Picture Description

String processing method

"方法"在编程中是一个专有名词
- "方法"特指<a>.<b>()风格中的函数<b>() 
- 方法本身也是函数,但与<a>有关,<a>.<b>()风格使用 
- 字符串或字符串变量是<a>,存在一些可用方法

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Formatted string type

格式化是对字符串进行格式表达的方式
- 字符串格式化使用.format()方法,用法如下: 
<模板字符串>.format(<逗号分隔的参数>)

Here Insert Picture Description

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
* Summary

- 正向递增序号、反向递减序号、<字符串>[M:N:K] 
- +、*、in、len()、str()、hex()、oct()、ord()、chr() - .lower()、.upper()、.split()、.count()、.replace() 
-- .center()、.strip()、.join()、.format()格式化
Published 16 original articles · won praise 0 · Views 426

Guess you like

Origin blog.csdn.net/qq_45627679/article/details/104359637