tuoheng_algN/test/语法/for/demo.py

43 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)
print(0b1101010)
value = int('1101010', 2)
print(value) # 106
value = int('125', 8)
print(value) # 85
value = int('3f2a1', 16)
print(value) # 258721
print(bin(106)) # 0b1101010 十进制转二进制
print(oct(85)) # 0o125 十进制转八进制
print(hex(258721)) # 0x3f2a1 十进制转十六进制
text = '1aa2'
print(text.split('a'))
# 1. 将字符串 "abcd" 转成大写
print("abcd".upper())
# 2. 计算字符串 "cd" 在 字符串 "abcd"中出现的位置
print("abcd".index('cd'))
print("abcd".find('cd'))
# 3. 字符串 "a,b,c,d" ,请用逗号分割字符串,分割后的结果是什么类型的?
print("a,b,c,d".split(','))
# 4. "{name}喜欢{fruit}".format(name="李雷") 执行会出错,请修改代码让其正确执行
print("{name}喜欢{fruit}".format(name="李雷", fruit='水果'))
# 5. string = "Python is good", 请将字符串里的Python替换成 python,并输出替换后的结果
print("Python is good".replace("Python", "python"))
# 6. 有一个字符串 string = "python修炼第一期.html",请写程序从这个字符串里获得.html前面的部分要用尽可能多的方式来做这个事情
print("python修炼第一期.html".split('.')[0])
# 7. 如何获取字符串的长度?
# 8. "this is a book",请将字符串里的book替换成apple
# 9. "this is a book", 请用程序判断该字符串是否以this开头
# 10. "this is a book", 请用程序判断该字符串是否以apple结尾
# 11. "This IS a book" 请将字符串里的大写字符转成小写字符
# 12. "This IS a book" 请将字符串里的小写字符,转成大写字符
# 13. "this is a book\n" 字符串的末尾有一个回车符,请将其删除