格式化输出

代码如下:

name = input("Name:")

age = input("Age:")

job = input("Job:")

salary = input("Salary")

msg = '''

------------------- info of %s ----------------------

Name: %s

Age: %s

Job: %s

Salary: %s

-------------------- end --------------------------

''' %(name,name,age,job,salary)

print(msg)

代码2如下:

name = input("Name:")

age =int( input("Age:"))  #将age转化为数字,后面可以计算

job = input("Job:")

salary = input("Salary")

if salary.isdigit(): #判断salary长的像不像数字

  salary = int(salary)

else:

  print("must input digit")

  exit() #退出程序

  #或者 exit("must input digit")

msg = '''

------------------- info of %s ----------------------

Name: %s

Age: %s  #%s改成%d 就只能输入数字

Job: %s

Salary: %s  #%s改成%d 就只能输入数字

You will be retired in %s years

-------------------- end --------------------------

''' %(name,name,age,job,salary,65-age)

print(msg)

#ctrl+? 选中的多行代码被注释

代码3如下:

name = input("Name:")

age =int( input("Age:"))  #将age转化为数字,后面可以计算

job = input("Job:")

salary = input("Salary")

if salary.isdigit(): #判断salary长的像不像数字

  salary = int(salary)

msg = '''

------------------- info of %s ----------------------

Name: %s

Age: %s  #%s改成%d 就只能输入数字

Job: %s

Salary: %d  #%s改成%d 就只能输入数字

You will be retired in %s years

-------------------- end --------------------------

''' %(name,name,age,job,salary,65-age)

print(msg)

#ctrl+? 选中的多行代码被注释

占位符

  %s   s = string 字符串

  %d  d = digit 整数

  %f  f = float 浮点数 就是小数

python学习:格式化输出的更多相关文章

  1. (Python )格式化输出、文件操作、json

    本节学习Python的格式化输出,文件操作以及json的简单用法 1.格式化输出 将非字符串类型转换成字符串,可以使用函数:str() 或者repr() ,(这两个函数的区别目前我还没搞懂,求解答) ...

  2. Python 基础 格式化输出

    Python 基础 格式化输出 现在需要我们录入我们身边好友的信息,格式如下: ------------ info of Alex Li ---------- Name : Alex Li Age : ...

  3. python的格式化输出

    Python的格式化输出有两种: 一.类似于C语言的printf的方法 二.类似于C#的方法

  4. python print格式化输出。

    python print格式化输出. 1. 打印字符串 print ("His name is %s"%("Aviad")) 效果: 2.打印整数 print ...

  5. Python之格式化输出,初始编码以及运算符

    一.题型 1.使用while循环输入 1 2 3 4 5 6  8 9 10 count = 0 while count < 10: count += 1   #count = count + ...

  6. Python的格式化输出,基本运算符,编码

    一. 格式化输出现在有以下需求,让用户输入name, age, job,hobby 然后输出如下所示: -----------info of Alex Li----------- Name : Ale ...

  7. python字符串格式化输出

    python格式化输出 python格式化输出有两种方式:百分号和format format的功能要比百分号方式强大,其中format独有的可以自定义字符填充空白.字符串居中显示.转换二进制.整数自动 ...

  8. Python print格式化输出

    python中的print格式化输出,基本格式:"[字符串]%格式1[字符串]%格式2[字符串]....."%(string1,string2.....) 格式符号 ------- ...

  9. Python之格式化输出讲解

    1.格式化输出整数python print也支持参数格式化,与C言的printf似, strHello = "the length of (%s) is %d" %(Hello W ...

  10. Python基本格式化输出

    什么叫格式化输出? 数据按照某种特殊的要求输出 假如输入一个整数,希望整数按照十六进制,八进制输出,如果输入一个小数,希望小数保留后面2位数然后输出,或者以科学计数法的方式来输出小数.字符串的输出希望 ...

随机推荐

  1. 应用调试(二)GDB

    title: 应用调试(二)GDBdate: 2019/1/17 21:00:10 toc: true 应用调试(二)GDB gdb下载工具安装交叉工具链设置GDB介绍编译GDBtarget/host ...

  2. MDK填充FLASH为0xFF

    title: MDK填充FLASH为0xFF date: 2019/1/3 20:34:05 --- MDK填充FLASH为0xFF 参考 area 指定地址 https://blog.csdn.ne ...

  3. ETL过程跑完后,使用python发送邮件

    目标库中,如果有行数为0的表,使用python发送邮件 # -*- coding:utf-8 -*- # Author: zjc # Description:send monitor info to ...

  4. React 记录(5)

    React文档:https://www.reactjscn.com/docs/state-and-lifecycle.html 慢慢学习:对照教程文档,逐句猜解,截图 React官网:https:// ...

  5. [物理学与PDEs]第5章习题1 矩阵的极分解

    证明引理 2. 1. 证明: (1)  先证明存在正交阵 ${\bf P},{\bf Q}$ 及对角阵 ${\bf D}$ 使得 $$\bex {\bf F}={\bf P}{\bf D}{\bf Q ...

  6. VS Code中编写C

    Visual Studio Code如何编写运行C.C++? Visual Studio Code的C/C++扩展功能 vscode配置C/C++的编译调试环境

  7. git切换到新的远程地址

    查看仓库链接 git remote -v 修改url链接 git remote set-url origin URL

  8. Linux配置日志服务器

    title: Linux配置日志服务器 tags: linux, 日志服务器 --- Linux配置日志服务器 日志服务器配置文件:/etc/rsyslog.conf 服务器端: 服务器IP如下: 编 ...

  9. warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

    在C++中, char* p = "abc"; // valid in C, invalid in C++ 会跳出警告:warning: ISO C++ forbids conve ...

  10. 微信小程序传递参数(字符串、数组、对象)

    [转自燕歆波]感谢! //通过提供的JSON.stingify方法,将对象转换成字符串后传递 click:function(e){ var model = JSON.stringify(e.curre ...