Python 学习第四天!

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_45652666/article/details/102714682

掌握 字符串与序列

字符串

字符串是 Python 中最常用的数据类型。我们可以使用引号( ’ 或 " )来创建字符串。
创建字符串很简单,只要为变量分配一个值即可。

Python 访问字符串中的值

Python 不支持单字符类型,单字符在 Python 中也是作为一个字符串使用。
Python 访问子字符串,可以使用方括号来截取字符串,如下实例:

var1 = 'Hello Boys!'
var2 = "apple"
 
print ("var1[0]: ", var1[0])
print ("var2[1:5]: ", var2[1:5])

结果:
var1[0]: H
var2[1:5]: pple

Python 字符串更新

可以截取字符串的一部分并与其他字段拼接,如下实例:

var1 = 'Hello World!'
 
print ("已更新字符串 : ", var1[:6] + '懒羊羊!')

结果:
已更新字符串 : Hello 懒羊羊!

Python转义字符

在需要在字符中使用特殊字符时,python用反斜杠()转义字符。如下表:
在这里插入图片描述

Python字符串运算符

在这里插入图片描述

a = "Hello"
b = "Python"
 
print("a + b 输出结果:", a + b)
print("a * 2 输出结果:", a * 2)
print("a[1] 输出结果:", a[1])
print("a[1:4] 输出结果:", a[1:4])
 
if( "H" in a) :
    print("H 在变量 a 中")
else :
    print("H 不在变量 a 中")
 
if( "M" not in a) :
    print("M 不在变量 a 中")
else :
    print("M 在变量 a 中")
 
print (r'\n')
print (R'\n')

结果如下:
a + b 输出结果: HelloPython
a * 2 输出结果: HelloHello
a[1] 输出结果: e
a[1:4] 输出结果: ell
H 在变量 a 中
M 不在变量 a 中
\n
\n

Python字符串格式化

Python 支持格式化字符串的输出 。尽管这样可能会用到非常复杂的表达式,但最基本的用法是将一个值插入到一个有字符串格式符 %s 的字符串中。

print ("不知 %s 之水 %s 来!" % ('黄河', '天上'))

结果如下:

不知 黄河 之水 天上 来!

python字符串格式化符号:
在这里插入图片描述
格式化操作符辅助指令:
在这里插入图片描述

Python三引号

python三引号允许一个字符串跨多行,字符串中可以包含换行符、制表符以及其他特殊字符。

Python 的字符串内建函数

capitalize()

Python capitalize()将字符串的第一个字母变成大写,其他字母变小写。

str = "this is string example from runoob....wow!!!"

print ("str.capitalize() : ", str.capitalize())

结果如下:
str.capitalize() : This is string example from runoob…wow!!!

center()

center() 方法返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格。
str.center(width[, fillchar])

str = "[www.runoob.com]"

print ("str.center(40, '*') : ", str.center(40, '*'))



结果如下:
str.center(40, '*') :  ************[www.runoob.com]************

count()

描述
count() 方法用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。
语法
count()方法语法:
str.count(sub, start= 0,end=len(string))
参数
sub – 搜索的子字符串
start – 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。
end – 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。
返回值
该方法返回子字符串在字符串中出现的次数。

str="www.runoob.com"
sub='o'
print ("str.count('o') : ", str.count(sub))

sub='run'
print ("str.count('run', 0, 10) : ", str.count(sub,0,10))

结果如下:
str.count(‘o’) : 3
str.count(‘run’, 0, 10) : 1

expandtabs()

描述
expandtabs() 方法把字符串中的 tab 符号(’\t’)转为空格,tab 符号(’\t’)默认的空格数是 8。
语法
expandtabs()方法语法:
str.expandtabs(tabsize=8)
参数
tabsize – 指定转换字符串中的 tab 符号(’\t’)转为空格的字符数。
返回值
该方法返回字符串中的 tab 符号(’\t’)转为空格后生成的新字符串。

str="www.runoob.com"
sub='o'
print ("str.count('o') : ", str.count(sub))
sub='run'
print ("str.count('run', 0, 10) : ", str.count(sub,0,10))
str = "this is\tstring example....wow!!!"
print ("原始字符串: " + str)
print ("替换 \\t 符号: " +  str.expandtabs())
print ("使用16个空格替换 \\t 符号: " +  str.expandtabs(16))

结果如下:
str.count(‘o’) : 3
str.count(‘run’, 0, 10) : 1
原始字符串: this is string example…wow!!!
替换 \t 符号: this is string example…wow!!!
使用16个空格替换 \t 符号: this is string example…wow!!!

index()

描述
index() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常。
语法
index()方法语法:
str.index(str, beg=0, end=len(string))
参数
str – 指定检索的字符串
beg – 开始索引,默认为0。
end – 结束索引,默认为字符串的长度。
返回值
如果包含子字符串返回开始的索引值,否则抛出异常。

str1 = "Runoob example....wow!!!"
str2 = "exam";
print (str1.index(str2))
print (str1.index(str2, 5))
print (str1.index(str2, 10))

结果如下:
7
7
Traceback (most recent call last):

File “”, line 5, in
print (str1.index(str2, 10))

ValueError: substring not found
还有很多的字符串内建函数需要我们去学习,在接下来的几天会逐步学习完每一个函数,一起加油!
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45652666/article/details/102714682