字符串拼接方式

   1  使用 + 拼接字符串

2  格式化输出:%s字符串 %d整数 %f浮点数 %%输出% %X-16进制 %r-原始字符串

3 str.format()

代码如下:

#coding:utf-8
#/usr/bin/python
"""
2018-11-03
dinghanhua
字符串拼接,格式化输出
"""
import time name = input('input name :') #输入姓名
age = int(input('input age:')) #输入年龄
nowtime = time.strftime('%Y%m%d %H:%M:%S',time.localtime()) #当前时间
'''使用 + 拼接字符串'''
#字符串连接
print('your name is '+name+',\nyour are '+str(age)+' years old. \ntime: '+nowtime)
'''
格式化输出
方式一: %s字符串 %d整数 %f浮点数 %%输出% %X-16进制 %r-原始字符串
方式二: str.format()
'''
#格式化输出
print("""your name is %s,
your are %d years old.
time: %s"""%(name,age,nowtime)) '''浮点数制定输出2位小数; %%输出%'''
percent = 50.5
print('percent is %.2f%%' % percent) #制定浮点数的小数位 '''%X'''
x = 0xf00
print('16进制:0x%X' % x) '''%s,%r的区别'''
print('str is %%s %s' % r'c:\user\local')
print('str is %%r %r' % r'c:\user\local')
# str.format()
str = """your name is {0},
your are {1} years old.
time: {2}"""
print(str.format(name,age,nowtime)) str2 = """your name is {name},
your are {age} years old.
time: {time1}"""
print(str2.format(name=name,age=age,time1=nowtime))

python入门6 字符串拼接、格式化输出的更多相关文章

  1. 【Python④】python恼人的字符串,格式化输出

    恼人的字符串 计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.由于计算机是美国人发明的,因此,最早只有127个字母被编码到计算机里,也就是大小写英文字母.数字和一些符号,这个编码 ...

  2. Python基础学习_01字符串的拼接(字符串的格式化输出)

    # 字符串的拼接 ---字符串的格式化输出 # 字符串的拼接 ---字符串的格式化输出 name = input("name:") age = input("age:&q ...

  3. python - 字符串的格式化输出

    # -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: study_2_str.py@ide: PyCharm Community ...

  4. #python str.format 方法被用于字符串的格式化输出。

    #python str.format 方法被用于字符串的格式化输出. #''.format() print('{0}+{1}={2}'.format(1,2,3)) #1+2=3 可见字符串中大括号内 ...

  5. Python中日期和时间格式化输出的方法

    本文转自:https://www.jb51.net/article/62518.htm 本文实例总结了python中日期和时间格式化输出的方法.分享给大家供大家参考.具体分析如下: python格式化 ...

  6. python基础之 while 逻辑运算符 格式化输出等

    1.while循环 while 条件: 循环体 while 条件: 循环体 else: 循环体 重点: 当条件为真的时候,就进入循环体,从上到下依次执行,执行完最后一条语句时,while并不是直接退出 ...

  7. 列表[‘hello’ , ‘python’ ,’!’ ] 用多种方法拼接,并输出’hello python !’ 以及join()在python中的用法简介

    列表[‘hello’ , ‘python’ ,’!’ ] 用多种方法拼接,并输出’hello python !’ 使用字符串链接的四种方法都可以创建 字符串拼接一共有四种方法,也可以应用到列表的拼接中 ...

  8. JSON字符串控制台格式化输出 java

    1.正常情况下返回的json数据格式如下: {"header":{"transSn":"e33128bb7622462ebfb2cbfcc46baa1 ...

  9. Python 入门 之 print带颜色输出

    Python 入门 之 print带颜色输出 1.print带颜色输出书写格式: 开头部分: \033[显示方式; 前景色 ; 背景色 m 结尾部分: \033[0m 详解: 开头部分的三个参数: 显 ...

随机推荐

  1. mysql数据库知识

    学而时习之,不亦说乎!                              --<论语> 数据库所有操作的总结.   1.mysql的数据库服务为mysqld.exe   windo ...

  2. WPF Binding的值转换器

    注意:值转换器中用于传入额外信息的参数 parameter 在 Binding 时使用 Binding 对象的 ConverterParameter 属性指定,但是设置了 ConverterParam ...

  3. PIE SDK元素的选择和取消选择

    1功能简介 在数据的查看等时候会用到元素的选择, 目前PIE SDK支持元素的选择和去取消选择功能,下面对这两种功能如何使用进行介绍. 2功能实现说明 2.1元素的选择 2.1.1 实现思路及原理说明 ...

  4. 2.5 Go错误处理

    defer import "fmt" func testDefer(){ defer fmt.Println() defer fmt.Println() fmt.Println() ...

  5. oracle执行update语句卡住不动

    一.问题探究 开发的时候debug到一条update的sql语句时程序就不动了,然后我就在plsql上试了一下,发现plsql一直在显示正在执行,等了好久也不出结果.但是奇怪的是执行其他的select ...

  6. python从字符串内取两个符号之间的内容

    #取字符串中两个符号之间的东东 def txt_wrap_by(self,start_str, end, html): start = html.find(start_str) if start &g ...

  7. 1个示例 学会 mvc 常用标签

    HtmlHelper用法大全3:Html.LabelFor.Html.EditorFor.Html.RadioButtonFor.Html.CheckBoxFor  @Html.***For:为由指定 ...

  8. 使用Access作数据库

    import java.sql.*; public class ConnectAccess { public static void main(String args[]){ ConnectAcces ...

  9. winform代码生成器(二)

    代码下载 地址 http://pan.baidu.com/s/1nuZjyat 接着说 上文继续说,这次我们要生成主从表. 此方用到了第三方的 控件 DevExpress 的Gridview .大家可 ...

  10. Mysql数据库死锁分析相关概念

    参考博客: mysql死锁问题分析(https://www.cnblogs.com/LBSer/p/5183300.html) mysql insert锁机制(http://yeshaoting.cn ...