程建霞

2020-05-25   阅读量: 639

python常用处理方法总结

扫码加入数据分析学习群

#字符串的常用操作方法:查找,修改,判断。
#查找
'''
1.find():检测某个字串是否包含在这个字符串中,如果在返回这个子串开始的下标,否则返回-1.、
2.index():和find一样从左侧开始查找,就是当子串不存在字符串中时会直接报错。
3.count():返回某个子串在字符串中出现的次数。
语法:
字符串序列。find(子串,开始位置下标,结束位置下标)
'''
str1="hellow world and itcast and itheima and Python"
#find:检测某个字串是否包含在这个字符串中,如果在返回这个子串开始的下标,否则返回-1.
print(str1.find('and')) #13
print(str1.find('and',15,30)) #24
print(str1.find('ands')) #-1,子串不存在。

#index:和find一样从左侧开始查找,就是当子串不存在字符串中时会直接报错。
print(str1.index('and')) #13
print(str1.index('and',15,30)) #24
print(str1.index('ands')) #index检索不存在的子串是直接报错。


# count:返回某个子串在字符串中出现的次数。
print(str1.count('and',15,30)) # 1
print(str1.count('and')) #3
print(str1.count('ands')) # 0

#rfind---和find一样,只是从右侧开始。
#rindex--和index一样,只是从右侧开始。
str1="hellow world and itcast and itheima and Python"
print(str1.rfind('and')) #36
print(str1.rfind('and',15,30)) #24
print(str1.rfind('ands')) #-1

print(str1.rindex('and')) #36
print(str1.rindex('and',15,30)) #24
#print(str1.rindex('ands'))----直接报错

#修改的三个主要方法:
#replace、split、join。
#replace---子串替换;
# split子串分割;
# join-用一个字符串合并成一个新的字符串。


#1.replace 需求:把and换成he---说明replace函数有返回值,返回值时修改后的字符串
str1="hellow world and itcast and itheima and Python"
new_str1=str1.replace('and','he')
new1_str1=str1.replace('and','he',1)
new2_str1=str1.replace('and','he',10)
#如果替换次数超出子串中出现的次数,表示替换全部
print(str1)
print(new_str1)
#-----调用了replace函数,发现原有的字符串数据没有改变,修改后的数据时replace函数的返回值。
#---说明字符串时不可变数据类型
#-数据是否可以改变可以划分为:可变类型和不可变类型。


#2.split()---分割,返回一个列表,丢失分割符号。
#语法:字符串序列。split(分割字符,分割次数)
str1="hellow world and itcast and itheima and Python"
list1=str1.split('and')
list1=str1.split('and',2)
print(list1)



# 3.join()---合并列表里的字符串数据为一个大字符串
mylist1=['aa','bb','cc','dd']
new_list1='...'.join(mylist1)
print(new_list1)

#1.大小写转换。
'''
1.capitalize---将字符串第一个字符转换成大写
2.title---将每个单词首字母转换成大写
3.lower---将字符串中的大写转小写
4.upper---将字符串中小写转大写。
'''
str1="hellow world and itcast and itheima and Python"
new_str1=str1.capitalize()
print(new_str1) #将字符串中第一个字符转成大写

new_str2=str1.title()
print(new_str2) #将字符串中所有字符首字母转成大写

new_str3=str1.lower()
print(new_str3) #将大写转换成小写

new_str4=str1.upper()
print(new_str4) #将小写转换成大写


#2.删除空白字符。
'''
1.lstrip():删除字符串左侧空白字符
2.rstrip():删除字符串右侧空白字符
3.strip():删除字符串两侧空白字符
'''
str1=" hello world and itcast and itheima and Python "
print(str1)

#1.lstrip:
new_str5=str1.lstrip()
print(new_str5)
#2.rstrip
new_str6=str1.rstrip()
print(new_str6)
#3.strip():
new_str7=str1.strip()
print(new_str7)


#3.左中右对其
'''
1.ljust:返回一个原串左对齐,并使用指定字符(默认空格)填充对应长度的新字符串。
2.rjust:返回一个原字符串右对齐,并使用指定字符(默认空格)填充对应长度的新字符串。
3.center:返回一个原字符串居中对齐,并使用指定字符(默认空格)填充对应值长度的新字符串。
语法:
字符串序列。ljust(长度,填充字符)
'''
#演示代码===python console
'''
1.str1='hello' 'hello'
str1.ljust(10) 'hello '
str1.ljust(10,'.') 'hello.....'
str1.rjust(10) ' hello'
str1.rjust(10,'.') '.....hello'
str1.center(10) ' hello '
str1.center(10,'.') '..hello...'

'''


#4.判断:判断真假,返回结果为真或假。
'''
1.startswith():检查字符串是否以指定子串开头,是则返回true,否则但会false。
如果设置开始和结尾下标,则在指定范围内检查
2.endswith():检查字符串是否以指定子串结尾,是则返回true,否则但会false。
如果设置开始和结尾下标,则在指定范围内检查
语法:
字符串序列.startswith(子串,开始位置,结束位置)
'''

mystr="hellow world and itcast and itheima and Python"
print(mystr.startswith('hellow'))
print(mystr.startswith('hellow',10,30))
print(mystr.startswith('l',10,30))
#endswith():
print(mystr.endswith('Python'))
print(mystr.endswith('Python',10,20))


#5.判断
'''
1.isalpha():如果非空字符串所有的字符都是字母则返回true,否则返回false
2.isdigit():如果字符串都是数字则返回true,否则返回false
3.isalnum():如果一个非空字符串虽有字符都是字母或者数字的话则返回true,否则返回false
4.isspace():如果字符串中都是空白则返回true,否则返回false。
'''
mystr1='hello'
print(mystr1.isalpha())
mystr2='a12345'
print(mystr2.isdigit())
mystr3='a12345'
print(mystr3.isalnum())
mystr4=' '
print(mystr4.isspace())

添加CDA认证专家【维克多阿涛】,微信号:【cdashijiazhuang】,提供数据分析指导及CDA考试秘籍。已助千人通过CDA数字化人才认证。欢迎交流,共同成长!
13.8557 5 1 关注作者 收藏

评论(0)


暂无数据

推荐课程