zoukankan      html  css  js  c++  java
  • python 格式化的三种方法

    python格式化的三种方法:

    1.%--formatting

    2.str.format()

    3.F--String

    # coding: utf-8
    # Author:Carson
    # Date :2019/11/13 10:41
    # Tool :PyCharm
    
    name = '李明'
    age = 26
    city = 'SuZhou'
    dict = {'name': '刘翔', 'age': 31, 'city': 'ShangHai'}
    
    # %_formatting
    print('the name is %s' % name)
    print('the name is %s, his age is %s, he come from %s' % (name, age, city))
    print('the name is %s, his age is %s, he come from %s' % (dict['name'], dict['age'], dict['city']))
    
    # str.format()
    print(2)
    print('the name is {}'.format(name))
    print('the name is {}, his age is {}, he come from {}'.format(name, age, dict['age']))
    print('the name is {0}, his age is {1}, he come from {2}'.format(name, age, city))
    print('the name is {0}, his age is {1}, he come from {2}'.format(dict['name'], dict['age'], dict['city']))
    print('the name is {name}, his age is {age}, he come from {city}'.format(name=dict['name'], age=dict['age'], city=dict['city']))
    print('the name is {name}, his age is {age}, he come from {city}'.format(**dict))
    
    # F-Strings
    print(3)
    print(f'the name is {name}, his age is {age}, he come from {city}')
    print(f'the name is {name}, his age is {dict["age"]}, he come from {city}')
    print(f'the name is {name}, his age is {age+2}, he come from {city}')
    print(f'the name is {name}, his age is {(lambda x: x**2) (4)}, he come from {city}')

    输出:

    the name is 李明
    the name is 李明, his age is 26, he come from SuZhou
    the name is 刘翔, his age is 31, he come from ShangHai
    2
    the name is 李明
    the name is 李明, his age is 26, he come from 31
    the name is 李明, his age is 26, he come from SuZhou
    the name is 刘翔, his age is 31, he come from ShangHai
    the name is 刘翔, his age is 31, he come from ShangHai
    the name is 刘翔, his age is 31, he come from ShangHai
    3
    the name is 李明, his age is 26, he come from SuZhou
    the name is 李明, his age is 31, he come from SuZhou
    the name is 李明, his age is 28, he come from SuZhou
    the name is 李明, his age is 16, he come from SuZhou
  • 相关阅读:
    Sybase:游标用法以及嵌套用法
    EasyUI:获取某个dategrid的所有行数据
    EasyUI:所有的图标
    Sybase:SAP IQ学习笔记
    Sybase:SybaseIQ的几个系统过程
    Sybase:解锁
    Python3:文件读写
    Android Studio 1.0.2 设置内存大小
    关于Android的margin(当前视图与周围视图的距离)和padding(当前视图与内部内容的距离)
    《Android Studio开发实战 从零基础到App上线》资源下载和内容勘误
  • 原文地址:https://www.cnblogs.com/xioawu-blog/p/12410759.html
Copyright © 2011-2022 走看看