name = 'hello,world,WORLD! 123,你好'

#capitalize()
#首字母大写,其他全部变成小写,没有iscapitalize()方法
print(name.capitalize())
#Hello,world,world! 123

#title(),istitle()
#把每个单词的首字母都变成大写
print(name.title())
#Hello,World,World! 123,你好
#判断是否是首字母大写
print(name.istitle())
#False

#upper(),isupper()
#全部变成大写
print(name.upper())
#HELLO,WORLD,WORLD! 123
#判断是否全是大写
print(name.isupper())
#False

#casefold()
#全部变成小写,没有iscasefold()方法
print(name.casefold())
#hello,world,world! 123

#lower(),islower()
#全部变成小写
print(name.lower())
#hello,world,world! 123
#判断是否全是小写
print(name.islower())
#False

#casefold()和lower()的区别,lower()只对 ASCII 也就是 'A-Z'有效,但是其它一些语言里面存在小写的情况就没办法了。
#文档里面举得例子是德语中'ß'的小写是'ss'(这个我也不懂):
#s = 'ß'
#s.lower() # 'ß'
#s.casefold() # 'ss'
#总结来说,汉语 & 英语环境下面,继续用 lower()没问题;要处理其它语言且存在大小写情况的时候再用casefold()。

#center()
#总共需要占40个字符串位置,name在居中,如果不够就用-补充,如果字符串够了就不改变
print(name.center(40,'-'))
#-------hello,world,WORLD! 123,你好--------


#ljust(),rjust()
#总共需要占40个字符串位置,name居左,其余用-补齐
print(name.ljust(40,'-'))
#hello,world,WORLD! 123,你好---------------

#总共需要占40个字符串位置,name居右,其余用-补齐
print(name.rjust(40,'-'))
#---------------hello,world,WORLD! 123,你好

 #zfill()
#占40个字符,不够的用0补齐
print('heLlo,world'.zfill(40))
#00000000000000000000000000000heLlo,world

#count()
#统计o的个数
print(name.count('o'))
#2
#统计下标为0到5中的o的个数
print(name.count('o',0,5))
#1

#index(),find() 推荐用find
print(name.index('o'))
print(name.find('o'))
#2个方法都是从左开始查o在name中的位置,如果有则返回下标,如果没有则find返回-1,index会报错
#4
#从右开始查找o在name中的位置,如果有则返回下标,如果没有则find返回-1,index会报错
print(name.rfind('o'))
print(name.rindex('o'))

#startswith()
#判断是否以某个字符或字符串开关
print(name.startswith('h'))

#True

#endswith()
#判断是否以某个字符或字符串结尾
print(name.endswith('h'))
#False
#判断是否以你好结尾
print(name.endswith('你好'))
#True

#expandtabs()
#把字符串中的 tab 符号('\t')转为空格,tab 符号('\t')默认的空格数是8。
name1 = 'a\tb\tc\td'
print(name1)
#a b c d
print(name1.expandtabs(0))
#abcd
print(name1.expandtabs(4))
#a b c d
print(name1.expandtabs(8))
#a b c d
print(name1.expandtabs(10))
#a b c d

#isalpha()
#判断name是否全是英文字母
print(name.isalpha())
#Flase
print('abc'.isalpha())
#True

当变量是汉字时判断可能有误,需要指定编码格式
a = '哈哈'
print(a.isalpha())
True
print(a.encode('utf-8').isalpha())
False


#isdigit()
#判断是否全是正整数字,只能是正整数,不能有小数点或负数
print('abc123'.isdigit())
#Flase
print('123'.isdigit())
#True

#isalnum()
#判断是否只包含字母和数字,不能包含汉字或特殊字符等
print(name.isalnum())
#False
print('admin123'.isalnum())
#True


#isdecimal()
#判断字符串是否只包含十进制字符
print('admin123'.isdecimal())
#Flase
print('0123'.isdecimal())
#True

#python中str函数isdigit、isdecimal、isnumeric的区别
num1 = '1'
print(num1.isdigit())
#True
print(num1.isdecimal())
#True
print(num1.isnumeric())
#True

num2 = '四'
print(num2.isdigit())
#False
print(num2.isdecimal())
#False
print(num2.isnumeric())
#True

import unicodedata
print([unicodedata.numeric(i) for i in ["〇","零","一","壱","二","弐","三","参","四","五","六","七","八","九","十","廿","卅","卌","百","千","万","亿"]])
#[0.0, 0.0, 1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 20.0, 30.0, 40.0, 100.0, 1000.0, 10000.0, 100000000.0]
#
print([int(unicodedata.numeric(i)) for i in ["〇","零","一","壱","二","弐","三","参","四","五","六","七","八","九","十","廿","卅","卌","百","千","万","亿"]])
#[0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 100, 1000, 10000, 100000000]
#


#isspace()
#判断字符串是否只包含空格
print('1 2 3'.isspace())
#False
print(' '.isspace())
#True


#isidentifier()
#判断是否是一个合法的变量名
print('*'*50)
print('a_1'.isidentifier())
#True


#join()
#使用.将后面的字符串连接起来
print('.'.join('abcd'))
#a.b.c.d
print('|'.join(['a','b','c','d']))
#a|b|c|d

>>> "-".join("GNU/Linux is great".split())
'GNU/Linux-is-great'

 

#strip(),lstrip(),rstrip()
#将两边的空格和换行符都切除
print(' hello,world\n'.strip())
#hello,world

#只将左边的空格和换行符切除
print(' hello,world\n'.lstrip())
#hello,world
#空行

#只将右边的空格和换行符切除
print(' hello,world\n'.rstrip())
# hello,world

#删除两端的字符串
name = 'abcdeba'
print(name.strip('ab'))
#cde

#translate()
#定义一个明文字符串from_str,和一个加密字符串to_str,定义两个字符串的转换关系,要求2个字符串长度必须相等
#将'abcd345'中存在的可转换字符进行转换,没有转换关系的字符保留
from_str = 'admin123'
to_str = '!@#$%^&*'
trans_table = str.maketrans(from_str,to_str)
print('abcd345'.translate(trans_table))
#!bc@*45


#partition()
#将字符串以最左面的o为中间值进行分隔,分隔成3段
print('helloworld'.partition('o'))
#('hell', 'o', 'world')

#rpartition()
#将字符串以最右面的o为中间值进行分隔,分隔成3段
print('helloworld'.rpartition('o'))
#('hellow', 'o', 'rld')

#replace()
#替换字符,将l替换成L
print('hello,world'.replace('l','L'))
#heLLo,worLd

#替换字符,将l替换成L且只替换1次
print('hello,world'.replace('l','L',1))
#heLlo,world


#split(),splitlines()
#以o为分隔进行切片,o自身会被切除
print('hello\nworld !'.split('o'))
#['hell', '\nw', 'rld !']

#可以指定切片次数
print('hello\nworld !'.split('o',1))
['hell', '\nworld !']

#默认以空格或者换行符进行切片
print('hello\nworld !'.split())
#['hello', 'world', '!']

a = '序号       部门      人数    平均年龄     备注'
print(a.split())
#['序号', '部门', '人数', '平均年龄', '备注']  有多个空格也可以一次分割

#以换行符进行切片
print('hello\nworld !'.splitlines())
#['hello', 'world !']

swapcase()
#返回字符串大小写交换后的版本
s = "I am A pRoGraMMer"
print(s.swapcase())
#'i AM a PrOgRAmmER'


回文检查
回文是一种无论从左还是从右读都一样的字符序列。比如 “madam”。在这个例子中,我们检查用户输入的字符串是否是回文,并输出结果。
s = input("Please enter a string: ")
z = s[::-1]
if s == z:
print("The string is a palindrome")
else:
print("The string is not a palindrome")

 

posted on 2018-10-22 10:59  longfei2021  阅读(147)  评论(0编辑  收藏  举报