python字符串

2019-07-31  本文已影响0人  举颗凤梨

1.什么是字符串(str)

a.普通字符:

字母/数字/各国的文字和符号等(可以写在引号中的符号)

b.转义字符:

在字符串中在一些特定的符号前加\来表示特殊功能和意义

c.编码字符:

\u+4位的十六进制数 - 将4位十六进制数对应的编码值转换为字符

1)字符编码

计算机只有直接存储数字的能力,不能直接存储字符:
当需要计算机存储支付的时候,实质存的是字符对应的固定的数字,这个数字就是字符在计算机中的编码
每一个字符和数字的对应关系叫做编码表

2)ASCII码表和Unicode编码表

3)字符编码相关方法

练习,如果字符串中想打印引号,可以在引号前加\

str1 = 'abc\'123'  # abc'123
str2 = 'abc\t123'  # abc    123
str3 = 'abc\\123'  # abc\123
str4 = '\u4e00'    # 一

2.字符串操作

1.获取字符 - 和列表获取元素一样

1)获取单个字符

2)字符串切片 - 和列表切片一样

3)遍历

练习:统计一个字符串中小写字母的个数

str2 = 'asdfgASDFGHasd'
count = 0
for str in str2:
   if 97 <= ord(str) <= 122:
       count += 1
print(count)

2.字符串操作

1) + 和 *

str1 = 'abc'
str2 = '123'
print(str1 + str2)  # abc123
print(str1 * 3)  # abcabcabc

2)==, !=

print('abc' == 'acb') # False
print('abc' == 'abc') # True

3)> < >= <=

print('abc' > 'bc')  # False
print('abcf' > 'abcd')  # True
print('何' > '哈')  # True

4)in/not in

print('ab' in 'abcdef')  # True
print('abcde' in 'abc')  # False
print('ab' in 'agseb')  # False

5)len, max, min, sorted, str

str3 = 'how are you!'
print(len(str3))
print(max(str3))  # y
print(sorted(str3))  # [' ', ' ', '!', 'a', 'e', 'h', 'o', 'o', 'r', 'u', 'w', 'y']

6).r语法

str4 = r'ad\netrwe\'df\tdjfi'
print(str4)  # ad\netrwe\'df\tdjfi

7)格式字符串

a.语法:

包含格式占位符的字符 % (数据1,数据2,...) - 后括号中数据的个数和类型要和前面格式占位符一一对应

b.格式占位符

name = input('请输入姓名')
print(name + ',你好!', '%s,你好!'%name)

format的用法

print('{}是一个{}岁的{}'.format('小米','23','女孩子'))   # 小米是一个23岁的女孩子
print('{0}是一个{2}岁的{1}'.format('小米','女孩子','23'))    # 小米是一个23岁的女孩子
print('{name}是一个{age}岁的{gender}'.format(age='23',name='小米',gender='女孩子'))   # 小米是一个23岁的女孩子

字符串的相关方法

1.对齐方式

str1 = 'abc'
print(str1.center(10,'+'))   # 居中:+++abc++++
print(str1.ljust(10,'+'))   # 左对齐:abc+++++++
print(str1.rjust(10,'+'))   # 左对齐:+++++++abc
print(str1.zfill(10))   # 左填充:0000000abc

2.统计子串的个数

str1 = 'how are you! Im fin thank you and you '
print(str1.count('you'))  # 3
print(str1.count('r'))   # 1
print(str1.count('you',0,-5))  # 2  在指定位置的you出现的次数

3.查找指定子串并返回下标

print(str1.find('you'))  # 8  不存在不报错
print(str1.index('you'))  # 8  不存在会报错

4.连接:join

new_str1 = '+'.join('123')
print(new_str1)     # 1+2+3

5.替换

str1 = 'how are you! Im fin thank you and you '
new_str1 = str1.replace('you','YOU')
new_str2 = str1.replace('you','YOU',2)
print(new_str1)   # how are YOU! Im fin thank YOU and YOU
print(new_str2)   # how are YOU! Im fin thank YOU and you

6.字符串切割

str1 = 'how are you! Im fine thank you and you  '
print(str1.split(' '))  # ['how', 'are', 'you!', 'Im', 'fine', 'thank', 'you', 'and', 'you', '', '']
print(str1.split('!'))  # ['how are you', ' Im fine thank you and you  ']
print(str1.split())  # ['how', 'are', 'you!', 'Im', 'fine', 'thank', 'you', 'and', 'you']

字符串中其它方法

str1 = 'how are you! Im fine thank you and you'
# 将字符串第一个字符大写
print(str1.capitalize())
# 字符串1.endwith(字符串2,开始位置,结束)
# 判断在字符串1中从开始到结束位置,字符串是否以指定字符结尾
print(str1.endswith('you'))   # True
print(str1.endswith('you',0,18))   # False
# 判断在字符串1中从开始到结束位置,字符串是否以指定字符开始
print(str1.startswith('you'))
# 字符串.isalnum()
# 判断一个不为空的字符串中是否所有字符都是字母或者数字
print(str1.isalnum())    #False
# 字符串.isalpha()
# 判断一个不为空的字符串中是否所有字符都是字母或者数字
print(str1.isalpha())    #False
# 判断一个不为空的字符串中是否只包含十进制数字
print(str1.isdecimal())    #False
# 判断一个不为空的字符串中是否只包含数字
print(str1.isdigit())    #False
# 判断一个不为空的字符串中是否全为小写
print(str1.islower())    #False
# 判断一个不为空的字符串中是否全为大写
print(str1.isupper())    #False
# 将字符串中所有小写字符转化为大写
print(str1.upper())     #HOW ARE YOU! IM FINE THANK YOU AND YOU
# 将字符串中所有大写字符转化为小写
print(str1.lower())    #how are you! im fine thank you and you
# 翻转字符串中的大小写
print(str1.swapcase())   # HOW ARE YOU! iM FINE THANK YOU AND YOU
# 删除字符串开始和末尾的空格,有r和l
print(str1.strip())
上一篇 下一篇

猜你喜欢

热点阅读