python 字符串

2021-01-14 10:20:26 浏览数 (1)

参考链接: Python字符串| printable

1.什么是字符串 

用引号引起来的一串字符用引号来创建字符串 

  单引号双引号三单引号三双引号  

引号使用需要注意匹配关系,且不可以换行,如果要换行,三单引号或者三双引号。 

pm = '''轻轻的我走了,

正如我轻轻的来。'''

print(pm)

轻轻的我走了, 正如我轻轻的来。 

2.字符串的运算及常见操作 

2.1拼接 

a = ["Hello"]b = [",Python."]

print(a b)

[‘Hello’, ‘,Python.’] 

拼接的对象必须是同一类型。 

2.2重复 

a="city college  "

print(a*3)

city college city college city college 

2.3索引(偏移) 

sr="Python"

print(len(sr))

for i in range(len(sr)):

    print(sr[i],end="")

Python 

2.4切片 

sr="Life is short,you need python."

print(len(sr))

print(sr[::-1]) #从 到 步长

30 .nohtyp deen uoy,trohs si efiL 

2.5大小写转换 

sr="Life is short,you need python."

print(sr.lower())    #小写

print(sr.upper())    #大写

print(sr.swapcase())   #大小写互换

print(sr.title())   #每个单词第一个字母大写

print(sr.capitalize())   #每句第一个字母大写

life is short,you need python. LIFE IS SHORT,YOU NEED PYTHON. lIFE IS SHORT,YOU NEED PYTHON. Life Is Short,You Need Python. Life is short,you need python. 

2.6验证码确认 

certid="AbCd"

ipt = input("请输入验证码:")

if ipt.lower() == certid.lower():

    print("输入成功")

else:

    print("输入错误,请重试")

请输入验证码:adcb 输入错误,请重试。 

2.7字符串的格式对齐 

# - sr.center([len],[填充符号]),居中对齐

# - sr.ljust([len],[填充符号]),居左对齐

# - sr.rjust([len],[填充符号]),居右对齐

# - sr.zfill([len],[填充符号]),居右对齐,默认填充0

sr = "Life is short,you need python."

print(sr.center(41,"#"))

print(sr.ljust(41,"#"))

print(sr.rjust(41,"#"))

print(sr.zfill(41))

######Life is short,you need python.##### Life is short,you need python.########### ###########Life is short,you need python. 00000000000Life is short,you need python. 

2.8删除指定字符 

# - sr.strip()

# - sr.lstrip()

# - sr.rstrip()

sr = "#####Life is short,you need python.#####"

print(sr.strip("#"))

2.9计数 

# - sr.count()

# ####01234567893423525252522

sr = "Life is short,you need python."

print(sr.count('o',11,14))     #sr[11]到sr[14]这个范围有多少个o

2.10字符串搜索定位和替换 

# - sr.find(),查找元素并返回第一次出现的元素的索引值,查不到返回-1

# - sr.index(),查找元素并返回第一次出现的元素的索引值,报错

# - sr.rindex, 从右往左查找

# - sr.replace([现有],[替新])

sr = "Life is short,you need python."

print(sr.find('z',19,25))

print(sr.index('z',19,25))

print(sr.rindex('e'))

print(sr.replace('t','T',1))

2.11字符串条件判断 

#字符串条件判断

# - isalnum 是否由数字或字母组成

# - isalpha 是否由字母组成

# - isdigits 是否由数字组成

a = "abc666def"

b = "abc"

print(a.isalpha())

False 

2.12制表符的转化 

sr = "Lifetis short,you need python."

print(sr.expandtabs())

Life is short,you need python. 

2.13字符串的分割变化 

#字符串的分割变化

# - join(),将指定字符插到元素之间

# - split(),以指定字符分割字符串并除去该字符

# - partition(),以指定字符分割字符串并保留该字符

sr = "Life is short,you need python."

print(' '.join(sr))

li =["I ","Love ","Python."]

print(''.join(li))

print(sr.split('o',2))

print(sr.partition('o'))

L i f e i s s h o r t , y o u n e e d p y t h o n . I Love Python. [‘Life is sh’, ‘rt,y’, ‘u need python.’] (‘Life is sh’, ‘o’, ‘rt,you need python.’) 

2.14ASCII值和字符转化 

#ord() 字符--->ASCII

#chr() ASCII--->字符

for i in range(ord('a'),ord('z') 1):

    print(chr(i),end=" ")

a b c d e f g h i j k l m n o p q r s t u v w x y z 

拓展 

#string模块

import string

print(dir(string))

print(string.ascii_letters)

print(string.ascii_lowercase)

print(string.ascii_uppercase)

[‘Formatter’, ‘Template’, ‘_ChainMap’, ‘_TemplateMetaclass’, ‘all’, ‘builtins’, ‘cached’, ‘doc’, ‘file’, ‘loader’, ‘name’, ‘package’, ‘spec’, ‘_re’, ‘_string’, ‘ascii_letters’, ‘ascii_lowercase’, ‘ascii_uppercase’, ‘capwords’, ‘digits’, ‘hexdigits’, ‘octdigits’, ‘printable’, ‘punctuation’, ‘whitespace’] abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 

八进制,十六进制: 

for i in range(8):

    print(oct(i),end=" ")

print()

for i in range(16):

    print(hex(i),end=" ")

print()

print('%o' % 17)   #八进制

print('%x' % 17)   #十六进制

0 人点赞