标签:序号 字符 pre 其他 输出 sep *** pil alpha
字符串数字、字母、下划线组成的一串字符,主要是用来描述一些类似于名字,爱好……
在单引号、双引号、三单/双引号之间
name1 = 'hades' # 单引号
name2 = "bonnie" # 双引号
name3 = '''hades and bonnie''' # 三单引号
name4 = """hades + bonnie""" # 三双引号
print(name1)
print(name2)
print(name3)
print(name4)
hades
bonnie
hades and bonnie
hades + bonnie
hades_info = "hades's weight is 125,height is 168 and he likes reading"
# 0123456789…… 索引序号
print(hades_info[3]) # 正值从左往右数
print(hades_info[-3]) # 负值从右往左数
e
i
语法:
hades_info = "hades's weight is 125,height is 168 and he likes reading"
print(hades_info[:])
print(hades_info[8:14])
print(hades_info[5:17:2]) # 如果起始索引 < 终止索引,则步长为正,默认值为1
print(hades_info[-7:-1:1])
print(hades_info[-1:-7]+ '打印不了') # **如果起始索引 > 终止索引,则步长一定需要设置且必须为负值,否则不会打印
print(hades_info[-1:-7:-1])
hades's weight is 125,height is 168 and he likes reading
weight
' egti
readin
打印不了
gnidae
注意:字符串里的空格也是一个字符
语法:
hades_info = "hades's weight is 125,height is 168 and he likes reading"
print(len(hades_info))
56
语法:
hades_info = "hades's weight is 125,height is 168 and he likes reading"
print('hades'in hades_info)
print('girl'in hades_info) # 这是进行判断,所打印bool值
True
False
语法:
string = ' a abcd a '
print(string)
print(string.strip())
string = 'aaaa abcd aa'
string1 = ' aaaa abcd aa'
print(string.strip('a'))
print(string1.strip('a'))
a abcd a
a abcd a
abcd
aaaa abcd
语法:
hades_info = "hades's weight is 125,height is 168 and he likes reading"
print(hades_info.split())
print(hades_info.split('s')) # 以's'为切分点,即遇到's'就切
print(hades_info.split('s',3)) # 这个切了3次
["hades's", 'weight', 'is', '125,height', 'is', '168', 'and', 'he', 'likes', 'reading']
['hade', "'", ' weight i', ' 125,height i', ' 168 and he like', ' reading']
['hade', "'", ' weight i', ' 125,height is 168 and he likes reading']
string = 'abcd f'
for i in string:
print(i) # pay attention 空格也是一个字符哦
a
b
c
d
f
语法&定义:用法和strip一样
string = ' a abcd a '
print(string)
print(string.lstrip())
print(string.rstrip())
a abcd a
a abcd a
a abcd a
语法&定义:
string = 'a 246 ABcd % G '
print(string.lower())
print(string.upper())
a 246 abcd % g
A 246 ABCD % G
语法&定义:
str = "this is string example....wow!!!";
print(str.startswith( 'this' ))
print(str.startswith( 'is', 2, 4 ))
print(str.startswith( 'this', 2, 4 ))
True
True
False
语法&定义:用法和split一样
hades_info = "hades's weight is 125,height is 168 and he likes reading"
print(hades_info.rsplit('s',3)) # 以's'为切分点,从右往左切了3次
["hades's weight i", ' 125,height i', ' 168 and he like', ' reading']
语法&定义:
alph_list = ['a','b','c','d']
str = ' > > '
print(str.join(alph_list))
print(type(str.join(alph_list)))
a > > b > > c > > d
<class 'str'>
注意:数据类型不可和字符串拼接
alph_list = ['a','b',3,'d']
str = ' > > '
print(str.join(alph_list)) # 报错
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-56-c81183775983> in <module>
1 alph_list = ['a','b',3,'d']
2 str = ' > > '
----> 3 print(str.join(alph_list)) # 报错
TypeError: sequence item 2: expected str instance, int found
语法&定义:
str
中的子字符串str1
替换成str2
hades_info = "hades's weight is 125,height is 168 and he likes reading"
print(hades_info.replace('is','是'))
hades's weight 是 125,height 是 168 and he likes reading
语法&定义:
str1 = '234'
str2 = ' 234'
print(str1.isdigit())
print(str2.isdigit())
True
False
语法&定义:
str
字符串中的某个子字符串str1
,True则返回字符串的索引值,False则返回-1str
字符串中的某个子字符串str1
,True则返回字符串的索引值,False则返回-1str
字符串中的某个子字符串str1
,True则返回字符串的索引值,False则报错str
字符串中的某个子字符串str1
,True则返回字符串的索引值,False则报错str
字符串中寻找某个子字符串str1
的个数str = 'abcdeafghijklmn'
print(str.find('b'),str.find('r'))
print(str.rfind('a'),str.rfind('r'))
print(str.index('k'),str.rindex('b'))
print(str.count('a'),str.count('r'))
1 -1
5 -1
11 1
2 0
语法&定义:
str = 'hades'
print(str.center(20,'*'))
print(str.ljust(20,'-'))
print(str.rjust(20,'$'))
print(str.zfill(20))
*******hades********
hades---------------
$$$$$$$$$$$$$$$hades
000000000000000hades
语法&定义:
\t
的位置添加空格,默认为8个空格print('a\tbc\te'.expandtabs())
print('a\tbc\te'.expandtabs(4))
a bc e
a bc e
语法&定义:
str = 'ab CDEfg hiJK,Lmn'
print(str.capitalize())
print(str.swapcase())
print(str.title())
Ab cdefg hijk,lmn
AB cdeFG HIjk,lMN
Ab Cdefg Hijk,Lmn
`1. 写代码,有如下变量(name = " aleX"),请按照要求实现每个功能:
name = ' aleX'
print(f"1.{name.strip()}")
print(f"2.{name.startswith('al')}")
print(f"3.{name.endswith('X')}")
print(f"4.{name.replace('l','p')}")
print(f"5.{name.split('l')}")
print(f"6.{name.upper()}")
print(f"7.{name.lower()}")
print(f"8.{name[1]}")
print(f"9.{name[:3]}")
print(f"10.{name[-2:]}")
print(f"11.{name.index('e')}")
print(f"12.{name[:-1]}")
1.aleX
2.False
3.True
4. apeX
5.[' a', 'eX']
6. ALEX
7. alex
8.a
9. al
10.eX
11.3
12. ale
标签:序号 字符 pre 其他 输出 sep *** pil alpha
原文地址:https://www.cnblogs.com/Hades123/p/10828047.html