字符类型
一、字符类型的定义
使用单引号定义一个字符
name = '哈哈'使用双引号定义一个字符
name = "哈哈"- 如果是多行的时候(常见写
SQL语句),就需要用三个单引号或者三个双引号来定义
二、字符常见的方法或者属性
1、
len()计算长度str1 = 'hello word' print(len(str1))2、根据字符位置取值
str1 = 'hello word' # 0表示字符的下标(如果超出了字符的长度就会报错) print(str1[0])3、切片
str1 = 'hello word' print(str1[0:len(str1)])4、判断字符是否属于一个字符里面(
in/not in)str1 = 'hello word' print('h' in str1)5、
strip()去除字符的前后空格str1 = ' hello word ' print(str1.strip())6、
split()分割字符date = '2018-06-24' date.split('-') # 分割后成为一个list ['2018', '06', '24']7、
lstrip()左边切割str1 = '*****哈哈****' print(str1.lstrip('*'))8、
rstrip()右边切割str1 = '*****哈哈****' print(str1.rstrip('*'))9、
upper()转换为大写(针对字符,不针对中文汉字)10、
lower()转换为小写(针对字符,不针对中文汉字)11、
startswith()以什么开头endswith()以什么结尾str2 = 'hello word' print(str2.startswith('hell')) print(str2.endswith('word'))12、
replace(需要替换的字符, 替换的字符, 替换几个)str2 = 'hello word' print(str2.replace('l', '@', 1))