「Python」2020.03.25学习笔记 | 第五章字符串-字符串运算、格式化、strip()、大小写互换、字符串对齐、搜索

  • 学习测试开发的Day83,真棒!
  • 学习时间为1H30M
  • 第八次全天课(下午视频1H3M-2H11M)

小题:写一个函数,2个参数,一个是字符串,一个是重复次数

自己的代码

def f(s,n)
    result=""
    for i in range(n):
        result+=s
    return result
s="#####"
n=5
print(f(s,n))

输出:

PS D:\0grory\day8> python .\sn.py
#########################
PS D:\0grory\day8>

老师的代码:

#小题:写一个函数,2个参数,一个是字符串,一个是重复次数
def mul_str(s,times):
    if not (isinstance(s,str) and isinstance(times,int)):
        return None
    result=""
    for i in range(times):
        result+=s
    return result

print(mul_str("ab",5))
print(mul_str("ab","5"))
print(mul_str(5,5))

输出:

PS D:\0grory\day8> python .\strtimes.py
ababababab
None
None
PS D:\0grory\day8>

字符串运算

image.png

字符串格式化

image.png
>>> "%d"%10
'10'
>>> "%d"%"10"
Traceback (most recent call last):
  File "", line 1, in 
TypeError: %d format: a number is required, not str
>>>
>>> "%.2f"%1.12345
'1.12'
>>> "%e"%22222.234234
'2.222223e+04'
>>> "%E"%23324234.23423
'2.332423E+07'
>>>

python 数据统计、网页开发

Java 后台

>>> "%o"%10
'12'
>>> "%x"%17
'11'
>>> "%x"%15
'f'
>>> "%X"%15
'F'
>>>

模板 Template

from string import Template
s=Template("There are ${key1}${key2}Quotations Symbols")
print(s.substitute(key2="Python",key1=3))
PS D:\0grory\day8> python .\template.py
There are 3PythonQuotations Symbols
PS D:\0grory\day8>

拿着作品,4个框架,1个网站

老师:作家(1年3-4本)

MVC:
view:展现层 a.html



gloyroad:${{s}}


controller 控制层:

@route("\index.html")
def index():
    s=data().getusername()
    render_template("a.html",s)

model 数据模型(操作数据库):

user.py
    data类;
        方法:
            操作数据库,增删改查
            返回一个数据

常用的字符串操作-strip()函数

image.png

lstrip() rstrip()函数

image.png
>>> "     abd    ".strip()
'abd'
>>> "   \n sdfsdf\r\t    ".strip()
'sdfsdf'
>>> "   \n sdfsdf\r\t    ".lstrip()
'sdfsdf\r\t    '
>>> "   \n sdfsdf\r\t    ".rstrip()
'   \n sdfsdf'
>>>

主要用在读入的时候,怕用户不小心多输入了一些东西

常用的字符串操作--大小写互换

image.png
>>> "AAAABBBasdf".upper()
'AAAABBBASDF'
>>> "AAAABBBasdf".lower()
'aaaabbbasdf'
>>> "AAAABBBasdf".capitalize()
'Aaaabbbasdf'
>>>
>>> command=input("please input")
please input
>>> command=input("please input Y/y:")
please input Y/y:
>>> if command.upper()=="Y":print("YY")
>>> dir("a")
['__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']
>>> import string
>>> string.capwords("ab cd ef")
'Ab Cd Ef'
>>>
>>> "l".isupper()
False
>>> "A".isupper()
True
>>> "a".islower()
True
>>> "1".isdigit()
True
>>> "1.1".isdigit()
False
## 是否由字母或数字组成
>>> "a1".isalnum()
True
>>> "a1".isalpha()
False
## 是否只由字母组成
>>> "a".isalpha()
True
>>> "A".isalpha()
True
##是否是空格
>>> " ".isspace()
True
>>>
## 将大小写字母互换
>>> "s D e V".swapcase()
'S d E v'
>>>

>>> dir(string)
['Formatter', 'Template', '_ChainMap', '_TemplateMetaclass', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_re', '_string', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation', 'whitespace']
>>> dir("a")
['__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']
>>>

常用的字符串操作--字符串对齐


image.png
## 左对齐
>>> "abc    ".ljust(10)
'abc       '
>>> "abc    ".ljust(1)
'abc    '
>>> ""==''
True
>>> "abc".ljust(10,"*")
'abc*******'
##右对齐
>>> "abc".rjust(10,"*")
'*******abc'
>>> "abc".rjust(10,"0")
'0000000abc'
>>> "abc".rjust(10)
'       abc'
>>>
## 居中对齐
>>> "abc".center(10,"*")
'***abc****'
>>>
>>> "abc".zfill(10)
'0000000abc'
>>>

常见的字符串操作--字符串的搜索

image.png
>>> "a" in "abc"
True
>>> "abc".find("c")
2
>>> "abc".find("x")
-1
>>>

子字符串出现的位置

>>> "abc".find("a",0)
0
>>> "abc".find("a",0,2)
0
>>> "abc".find("a",0,1)
0
>>> "abc".find("a",0,0)
-1
>>> "abc".find("b",0,1)
-1
>>> "abc".find("ab",0,1)
-1
>>> "abc".find("ab",0,2)
0
>>>

你可能感兴趣的:(「Python」2020.03.25学习笔记 | 第五章字符串-字符串运算、格式化、strip()、大小写互换、字符串对齐、搜索)