python 学习

拼接字符串

# 定义常量(全大写命名)
BASE_URL = "https://api.example.com/v1/"
FILE_PATH = "/data/user/"

# 引用常量拼接字符串
user_id = "123"
full_url = BASE_URL + "users/" + user_id  # 使用加号拼接
print(full_url)  # 输出: https://api.example.com/v1/users/123

# 使用 f-string 格式化(推荐)
full_url_f = f"{BASE_URL}users/{user_id}"
print(full_url_f)  # 输出: https://api.example.com/v1/users/123

# 使用 format 方法
full_url_format = "{base}users/{id}".format(base=BASE_URL, id=user_id)
print(full_url_format)  # 输出: https://api.example.com/v1/users/123

# 拼接文件路径
file_name = "profile.txt"
full_file_path = FILE_PATH + file_name
print(full_file_path)  # 输出: /data/user/profile.txt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值