python中的字符串拼接

# 字符串拼接
print('你还'+'挺傻的')   # +连接
name = '假如我是一棵树'
next1 = '一半埋在土里'
num = 123
print(name+','+next1+',一半长在风中')
# 字符串拼接不能拼接数字
print(name+','+next1+',一半长在风中'+str(num))
name1 = "如果有来生, %s" % name
print(name1)
name1 = "如果有来生, %s, %s" % (name, next1)
print(name1)

# f"内容{变量}"的格式来快速格式化
name = "你好"
set_year = 2023
price = 19.99
print(f"我是{name},今年是{set_year},价格是{price}")

格式化

name = "传智播客"
stock_price = 19.99
stock_code = "003032"
stock_price_daily_growth_factor = 1.2
growth_days = 7
print(f"公司:{name},股票代码:{stock_code},当前股价:{stock_price}")
# print(f"每日增长系数是:{stock_price_daily_growth_factor},经过了{growth_days}天的增长后,股价达到了:{stock_price*stock_price_daily_growth_factor**7}")
print("每日增长系数是:%.1f,经过%d天的增长后,股价达到了:%.2f" % (stock_price_daily_growth_factor, growth_days, stock_price*stock_price_daily_growth_factor**7))

# input()默认输入的类型是字符串类型
print("你是谁?")
name = input()
print(f"我是:{name}")

name1 = input("请告诉我你是谁?")
print(f"我知道啦,你是{name1}")

# 输入数字类型
num = input("pwd是多少?")
print("密码是:", type(num))  # 
num = int(num)
print("密码是:", type(num))  # 

你可能感兴趣的:(python,开发语言)