William126

Python实战之字符串的详细简单练习

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__',
 '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__',
 '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
  '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
   'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format',
    'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower',
    'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip',
    'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip',
     'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

name = "william"
new_name = "WILLLIAM"
print(name.capitalize())
# William,capitalize首字母变大写
print(new_name.casefold())
# willliam,casefold 大写全变小写

print(name.center(20,"*"))
#输出******william*******
print(name.count('l'))
#2,统计字符串中l的个数

print(name.encode())
#b'william',将字符串编码成bytes的格式

print(name.endswith('m'))
#True,检查字符串是否以m结尾

print("william\tzhang".expandtabs(6))
#william     zhang,将\t转化为长度为6的空格

print(name.find('w'))
print(name.find('b'))
#0;-1,返回找到的第一个值的索引,如果没有则返回-1


format:
    msg = "my name is {},and age is {}".format('William',20)
    print(msg)
    # my name is William,and age is 20
    msg1 = "my name is {1},and age is {0}".format('William',20)
    print(msg1)
    # my name is 20,and age is William
    msg2 = "my name is {name},and age is {age}".format(age=18,name='William')
    print(msg2)
    # my name is William,and age is 18
    msg3 = "my name is {name},and age is {age}".format_map({'name':'william','age':18,})
    print('*'*20)
    print(msg3)
    # my name is william,and age is

name = "William"
print(name.index('l'))
#2,返回字符串l的索引值

print(name.isnumeric())
#False
print(name.isprintable())
#True
print(name.isspace())
#False
print(name.istitle())
#True
print(name.isupper())
# False


print("|".join(['zhangsan','lisi','wangermazi']))
# zhangsan|lisi|wangermazi
print(" ".join(['zhangsan','lisi','wangermazi']))
# zhangsan lisi wangermazi

outtab = '1'
trantab = str.maketrans(intab,outtab)
print(trantab)
# {53: 49}
str = "15135"
print(str.translate(trantab))
# 11131
# 把数字5变成数字1
msg = "William is a good boy"
print(msg.partition("is"))
# ('William ', 'is', ' a good boy')
# 用字符串'is'将msg分开


msg = "William is a good boy"
print(msg.replace('boy','man'))
# William is a good man,替换

print(msg.swapcase())
# wILLIAM IS A GOOD BOY,大小写互换

msg = "William is a good boy"
print(msg.replace('boy','man'))
# William is a good man

print(msg.swapcase())
# wILLIAM IS A GOOD BOY

print(msg.zfill(10))
# William is a good boy
print(msg.zfill(30))
# 000000000William is a good boy,让整个字符串长度为30

print(msg.ljust(30,'*'))
# William is a good boy*********,左面对其,右面*号补齐
print(msg.rjust(30,"*"))
# *********William is a good boy,右边对齐,左边*号补齐

 

#The second learning
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

word = "william is a good boy."
print(word.capitalize())
# William is a good boy.

print(word.center(100,"*"))
# ***************************************william is a good boy.***************************************

print(word.count("i"))
# 3
print(word.count("i",9))
# 0

print(word.encode(encoding='utf-8'))
# b'william is a good boy.'

print(b'william is a good boy.'.decode())
# william is a good boy.

print(word.endswith("y."))
# True


# word = "william is a good boy.\t,llll"
print(word.expandtabs(9))
# william is a good boy.     ,llll


print(word.find("is",11))
# -1
print(word.find("is"))
# 8

print(word.index('oo'))
# 14,if cant find,raise error

word = "William"
print(word.isalnum())
# True

print(word.islower())
# True

print(word.isspace())
print("*"*20)
print(word.istitle())

print("---".join(word))
# W---i---l---l---i---a---m

word = " William is a good boy"
print(word.ljust(30,"1"))
# William11111111111111111111111
print(word.lower())
# william

print(word.lstrip())
# William

print(word.partition('is'))
# (' William ', 'is', ' a good boy')
print(word.partition('hahaha'))
# (' William is good boy', '', '')

print(word.replace('o',"W"))
# William is a gWWd bWy
print(word.split('is'))
# [' William ', ' a good boy']
print(word.split('o',maxsplit=2))
# [' William is a g', '', 'd boy']
word = '''William is
         a good boy'''
print(word.splitlines(keepends=True))
# ['William is\n', '         a good boy']
print(word.splitlines(keepends=False))
# ['William is', '         a good boy']

print("|"*30)
word = "    william  "
print(word.startswith('w'))
# True
print(word.strip())
# william

word = "WIlliam"
print(word.swapcase())
# wILLIAM

print(word.title())

print(word.upper())

print(word.zfill(20))
print("*"*20)
x = "william w 1 445"
y = "  Alex william"
print(x.__add__(y))
print(y.__add__(x))
print(y.__contains__(x))
print(x.__eq__(y))
# print(x.__getattribute__("w"))
print("*" * 30)
print(x.__repr__())

 

posted on 2017-06-23 16:23  William126  阅读(200)  评论(0编辑  收藏  举报

导航