今天要和大家分享的,也是几个很基础但是很常用的小练习,写代码的时候我们经常会涉及到字符串的判断,大小写转换等,现在就来跟着一起看看怎么用Python实现吧~
一、文件IO
def file_io():
with open("huixing.txt", "wt") as out_file:
out_file.write("该文本写入到文件中\n看到我了吧!")
with open("huixing.txt", "rt") as in_file:
text = in_file.read()
print(text)
file_io()
二、字符串判断
def str_test():
# str = "www.liuyude.com"
str = "Liuyude"
print(str.isalnum()) # 判断所有字符都是数字或者字母
print(str.isalpha()) # 判断所有字符都是字母
print(str.isdigit()) # 判断所有字符都是数字
print(str.islower()) # 判断所有字符都是小写
print(str.isupper()) # 判断所有字符都是大写
print(str.istitle()) # 判断所有单词都是首字母大写,像标题
print(str.isspace()) # 判断所有字符都是空白字符
print("-" * 40)