1、实现计算求最大公约数和最小公倍数的函数
def gcd(x, y):
(x, y) = (y, x) if x > y else (x, y)
for factor in range(x, 0, -1):
if x % factor == 0 and y % factor == 0:
return factor
def lcm(x, y):
return x * y // gcd(x, y)
2、实现判断一个数是不是回文数的函数
a = 1234567
a_st = str(a)
print(a_st[::-1])
3、字符串操作
# 将字符串以指定的宽度居中并在两侧填充指定的字符
print(str1.center(50, '*'))
# 将字符串以指定的宽度靠右放置左侧填充指定的字符
print(str1.rjust(50, ' '))
******************hello, world!*******************
hello, world!
str2 = 'abc123456'
# 检查字符串是否由数字构成
print(str2.isdigit()) # False
# 检查字符串是否以字母构成
print(str2.isalpha()) # False
# 检查字符串是否以数字和字母构成
print(str2.isalnum()) # True
# 获得字符串修剪左右两侧空格的拷贝
print(str3.strip())
来源:https://blog.csdn.net/qq_37495916/article/details/98945201