string
python 中的 string 字符串要么是以 单引号
要么是 双引号
示意的,比如说:’hello’ 和 “hello” 是一个意思,也能够间接应用 print
打印字符串。
<code class="py"> print("Hello") print('Hello')
将 string 赋给变量
将一个字符串赋给一个变量能够应用上面的赋值语句。
<code class="py"> a = "Hello" print(a)
多行string
能够应用三个双引号的多行字符串格局赋给一个变量,如下所示:
<code class="py"> a = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.""" print(a) PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
同样也能够应用三个单引号形式。
<code class="py"> a = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.''' print(a)
string 和 array
像很多编程语言一样, python 中的字符串也是用 unicode 示意的字节数组,然而 Python 中并没有字符类型,因为 python 中的单引号和双引号都示意字符串。
应用 []
的模式能够拜访 string 中的元素,比方获取上面string 中的第一个字符。
<code class="py"> a = "Hello, World!" print(a[1])
循环string
因为 string 是一个 array,所以咱们能够应用 循环 来迭代string中的字符,python 中 应用 for 循环即可。
<code class="py"> for x in "banana": print(x)
string 长度
能够用 len()
函数获取 string 的长度
<code class="py"> a = "Hello, World!" print(len(a))
字符串查看
如何想查看 一个短语 或者 一个字符 是否为某一个字符串的子集,能够应用 in 操作符。
<code class="py"> txt = "The best things in life are free!" print("free" in txt) ---- output ---- PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py True
也能够将 in 和 if 搭配应用,实现条件判断逻辑。
<code class="py"> txt = "The best things in life are free!" if "free" in txt: print("Yes, 'free' is present.") --- output --- PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py Yes, 'free' is present.
同样的情理,能够应用 if not
实现下面状况的反向操作。
<code class="py"> txt = "The best things in life are free!" print("expensive" not in txt) txt = "The best things in life are free!" if "expensive" not in txt: print("Yes, 'expensive' is NOT present.") --- output --- PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py True Yes, 'expensive' is NOT present.
切片操作
应用 切片
语法 能够实现从一个 string 中返回一个范畴的子字符串。
通过指定切片的三元素:起始,完结 和 “:” ,来返回 string 的一部分,举个例子:从上面字符串中切下 第 2-5
地位的子串。
<code class="py"> b = "Hello, World!" print(b[2:5]) ---- output ---- PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py llo
从 start 地位切片
如果疏忽开始地位的话,这个切片范畴默认会从第一个字符开始,上面的例子和下面是一样的。
<code class="py"> b = "Hello, World!" print(b[:5])
从 end 地位切片
如果疏忽完结地位的话,这个切片范畴默认会到 string 的开端,比方上面的例子中从 string 的第二个地位开始切下字串。
<code class="py"> b = "Hello, World!" print(b[2:])
正数索引
正数示意从 string 的开端往前开始计算地位,举个例子:
<code class="py"> b = "Hello, World!" print(b[-5:-2]) --- output --- PS E:\dream\markdown\python> & "C:/Program Files (x86)/Python/python.exe" e:/dream/markdown/python/app/app.py orl
略微解释一下:
- From: ‘o’ 是 字符串中倒数第五个地位。
- To: ‘l’ 是 字符串中倒数第二个地位。
译文链接:https://www.w3schools.com/pyt…
更多高质量干货:参见我的 GitHub: python