#String和文件处理——EX11-14

  • String的使用
script, user_name = argv #argv: Argument variable参数变量
print(f"Hi {user_name}, I'm the {script} script.")

如上,f-string是3.6推出的,很遗憾,我的3.5版本不能使用,但是可以用str.format()

print("Hi {}, I'm the {} script.".format(user_name,script))

  • 传输
from sys import argv #把sys模组(module)import进来

想对python脚本传参数,那么就要就要使用命令行参数的支持,省得每次去改脚本。
输入的argv参数被视作string类型
在传输时,要注意要unpack的个数应该与Powershell中输入的参数个数匹配,否则会出现如下提醒:
这里写图片描述
也要注意这里运行的代码不再只是python ex11.py, 而变成了python ex11.py str1 str2 str3 (因为这里我们定义过4个argv)

from sys import argv
# read the WYSS section for how to run this
script, first, second, third = argv

print("The script is called:", script)
print("The first variable is:", first)
print("The second variable is:", second)
print("The third variable is:", third)

  • Input函数的使用
pompt = '>'
likes = input(pompt) #输出“>_________”输入内容后赋值给likes
age = input("How old are you?")
height = input("How tall are you?")
weight = input("How much do you weigh?") 

print(f"So, you're {age} old, {height} tall and {weight} heavy")

Exemple

from sys import argv

script, user_name = argv
prompt = '>'

print("Hi {}, I'm the {} script.".format(user_name,script))
print("I'd like to ask you a few questions.")
print("Do you like me {}?".format(user_name))
likes = input(prompt)

print("Where do you live {}?".format(user_name))
lives = input(prompt)

print("What kind of computer do you have?")
computer = input(prompt)

print("""
Alright, so you said {} about liking me. 
You live in {}. Not sure where that is.
And you have a {} computer. Nice""".format(likes,lives,computer)) 

Powershell运行结果:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值