PYTHON 的学习笔记
变量学习
python 的变量不用提前定义分配空间,直接使用。
*变量是标签
练习 2.1
//定义my_str 打印
my_str = '这是一串字符串'
print(my_str)
练习 2.2
my_str = '这是一串字符串'
print(my_str)
my_str = '这是新的一串字符串'
print(my_str)
字符串学习
字符串可以是单引号(')也可以是双引号(")
字符串的大小写
my_word_str = "it's new string"
print(my_word_str.title())
///输出 It'S New String
title的方法定义:
def title(self, *args, **kwargs): # real signature unknown
“”"
Return a version of the string where each word is titlecased.
More specifically, words start with uppercased characters and all remaining
cased characters have lower case.
“”"
pass
其中title 是string 的方法,title后面的()表示可以添加额外的信息。这个方法不需要咱们都不添加了。
注意,字符串如果出现特殊符号、空格啥的后面的小写字符也会变成大写
字符串的大小写的方法学习
my_upper_str = my_word_str.upper()
print(my_upper_str)
print(my_upper_str.lower())
输出:
IT IS NEW STRING
it is new string
变量替换字符串
first_name = '王'
last_name = '老二'
full_name = f"\t{first_name} \t {last_name}"
print(f"{first_name.title()}{last_name}")
print(full_name)
输出:
王老二
王 老二
知识点:\t可以增加空白。
删除空白
first_name = '王'
last_name = '老二'
full_name = f"\t{first_name}\t{last_name} "
print(f"{first_name.title()}{last_name}")
print(full_name)
full_name_strip = full_name.strip()
print(f"full_name_strip:{full_name_strip}")
输出:
王老二
王 老二
full_name_strip:王 老二
知识点:空白在字符串的开始和结尾可以删除,中间不行。\t 后面添加\n换行,在执行删除空白方法的时候也不会删除。
删除前缀和后缀
first_name = '王'
last_name = '老二'
full_name = f"{first_name}{last_name}."
full_name_remove = full_name.removesuffix('.')
full_name_preremove = full_name.removeprefix('王')
print(full_name_remove)
print(full_name_preremove)
输出:
王老二
老二.
练习 2.3:
def print_learn(name):
return f"Hello {name} ,你今天学习么?"
if __name__ == '__main__':
# print_hi('PyCharm')
print(print_learn('小王'))
输出:
Hello 小王 ,你今天学习么?
练习 2.4:
def print_upper_lower(name):
return {'upper': f'{name.upper()}', 'lower': f'{name.lower()}', 'title': f'{name.title()}'}
if __name__ == '__main__':
# print_hi('PyCharm')
# print(print_learn('小王'))
result = print_upper_lower('ale')
print(f'大写:{result.get("upper")},小写:{result.get("lower")},首字符大写:{result.get("title")}')
输出:
大写:ALE,小写:ale,首字符大写:Ale
练习 2.5 名言1
def print_people_say(*args):
if len(args) < 1:
print(f"The best way to get started is to quit talking and begin doing. --Walt Disney")
else:
sentence = args[0]
people = args[1]
print(f"{sentence} --{people}")
if __name__ == '__main__':
# print_hi('PyCharm')
# print(print_learn('小王'))
# result = print_upper_lower('ale')
# print(f'大写:{result.get("upper")},小写:{result.get("lower")},首字符大写:{result.get("title")}')
print_people_say()
say = f"The pessimist sees difficulty in every opportunity. The optimist sees opportunity in every difficulty"
people_name = "Winston Churchill"
print_people_say(say, people_name)
输出:
The best way to get started is to quit talking and begin doing. --Walt Disney
The pessimist sees difficulty in every opportunity. The optimist sees opportunity in every difficulty --Winston Churchill
练习 2.7
def delete_name_strip(name):
print(name)
print(f'删除右空白:{name.rstrip()}')
print(f'删除左空白:{name.lstrip()}')
print(f'删除所有空白:{name.strip()}')
if __name__ == '__main__':
# print_hi('PyCharm')
# print(print_learn('小王'))
# result = print_upper_lower('ale')
# print(f'大写:{result.get("upper")},小写:{result.get("lower")},首字符大写:{result.get("title")}')
# print_people_say()
# say = f"The pessimist sees difficulty in every opportunity. The optimist sees opportunity in every difficulty"
# people_name = "Winston Churchill"
# print_people_say(say, people_name)
delete_name_strip('\tale\n')
输出:
ale
删除右空白: ale
删除左空白:ale
删除所有空白:ale
练习2.8
def remove_file_name(filename):
print(filename.removesuffix('.txt'))
if __name__ == '__main__':
# print_hi('PyCharm')
# print(print_learn('小王'))
# result = print_upper_lower('ale')
# print(f'大写:{result.get("upper")},小写:{result.get("lower")},首字符大写:{result.get("title")}')
# print_people_say()
# say = f"The pessimist sees difficulty in every opportunity. The optimist sees opportunity in every difficulty"
# people_name = "Winston Churchill"
# print_people_say(say, people_name)
# delete_name_strip('\tale\n')
remove_file_name('people_say.txt')
变量之字符串学习结束!