<code class="language-python"># 用户交互 <a href="https://www.gaodaima.com/tag/input" title="查看更多关于input的文章" target="_blank">input</a>、格式化输出 # 1.接受用户的输入 # 在python3:input会将用户输入的所有内容都存成字符串类型 # input(): 要求用户必须输入一个明确的数据类型,输入的是什么类型,就存成什么类型 # <a href="https://www.gaodaima.com/tag/username" title="查看更多关于username的文章" target="_blank">username</a> = input("请输入您的账号:") # hah # print(username, type(username)) # hah <class "str"> # # <a href="https://www.gaodaima.com/tag/age" title="查看更多关于age的文章" target="_blank">age</a> = input("请输入的你的年龄: ") # age="18" # print(age, type(age)) # 18 <class "str"> # age = int(age) # int只能将纯数字的字符串转成整型 # print(age, type(age)) # 18 <class "int"> # 2.字符串格式化输出 # 2.1 % name = "阿旺" age = 19 print("名字是%s,年龄是%s" % (name, age)) # 只能是元组不能是列表 # %d print("%5d"%1.5) # %f # 2.2 format()---固定的 {} # 2.2.1顺序填坑 # 可以有元素多,不能有元素少! print("名字是 {},年龄是 {}".format(name ,age)) # 2.2.2下标填坑 # 不能下标越界 IndexError: tuple index out of range print("名字是 {1},年龄是 {0}".format(name ,age)) # 2.2.3变量方法 print("名字是 {name},年龄是 {age}".format(name="tom" ,age = 18)) # 2.3.4指定长度输出: # 1- {:长度} # 1- 数值型:右对齐,左补齐 # 2- 字符串:左对齐,右补齐 # 2- > 右对齐 # 3- < 左对齐 # 4- ^ 中间对齐 ---异或 # 5- 数值补0 ,一般是右对齐 , 左补0 ,不改变值 # 6- 字符串本身带花括号 {{}} # 2.3 f"" print(f"名字是{name},年龄是{age}") </code>
www#gaodaima.com来源gaodai#ma#com搞*代#码网搞代码