#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:cjs
@file: shopping.py
@time: 2020/05/27
需求:
1、启动程序后,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否足够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
字符串所有操作:
#!/usr/bin/env python
#-*- coding:utf-8 _*-
"""
@author:cjs
@file: string.py
@time: 2020/05/28
"""
name = "my name is {name} and my age is {age} years old"
print(name.capitalize()) ###首字母大写
print(name.count("e")) ###计数,有多少个"e"
print(name.center(100,"-")) ###打印100个字符,不够用"-"补齐
print(name.endswith("ex")) ###以ex结尾,如果是则打印True
print(name.expandtabs(tabsize=30)) ###扩展tab健
print(name.find("n")) ###找到字符的索引
print(name.format(name="alex",age=23)) ###字符串格式化
print(name.format_map({'name':"alex","age":12}))
print('ab23'.isalnum()) ###是否包含阿拉伯数字
print("abA".isalpha())
print("1A".isdecimal())
print("1A".isdigit()) ###是否为整数
print("1A".isidentifier()) ###判断是不是一个合法的标识符和变量名
print("33A".isnumeric()) ###检测字符串是否只由数字组成
print(" ".isspace()) ###检测是否为空格
print("My Name Is ".istitle()) ###判断首字母是否为大写
print("My Name Is".isprintable()) ###是否可打印
print("My Name Is".isupper()) ###是否全部为大写
print("-".join(["1","2","3","4"])) ###用符号隔开字符串
print(name.ljust(100,"*")) ###左边不够100字符右边补上
print(name.rjust(100,"-")) ###右边不够100字符左边补上
print("Alex".lower()) ###全部小写
print("Alex".upper()) ###全部大写
print("\nAlex".lstrip()) ###去掉左边空格后回车
print("Alex\n".rstrip()) ###去掉右边空格后回车
print(" Alex ".strip()) ####全掉全部空格后回车
p = str.maketrans("abcdefgh","12345678")
print("alex li".translate(p)) ###加密规则
print("alex li".replace("l","L",1)) ###替换字符串的l字符
print("alex li".rfind("l")) ###从右查找字符的索引
print("1+2+3+4".split("+")) ###提取符号
print("1+2\n+3+4".splitlines())
print("Alex Li".swapcase())
print("lex li".title())
print("lex li".zfill(50)) ###补位
D:\Python3.5.2\python.exe "D:/lesson 14/day2/string.py"
My name is {name} and my age is {age} years old
5
--------------------------my name is {name} and my age is {age} years old---------------------------
False
my name is {name} and my age is {age} years old
3
my name is alex and my age is 23 years old
my name is alex and my age is 12 years old
True
True
False
False
False
False
True
True
True
False
1-2-3-4
my name is {name} and my age is {age} years old*****************************************************
-----------------------------------------------------my name is {name} and my age is {age} years old
alex
ALEX
Alex
Alex
Alex
1l5x li
aLex li
5
['1', '2', '3', '4']
['1+2', '+3+4']
aLEX lI
Lex Li
00000000000000000000000000000000000000000000lex li
Process finished with exit code 0
来源:oschina
链接:https://my.oschina.net/u/4389538/blog/4294906