程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[learn Python from simple to deep] function 5 format function: str.format

編輯:Python

One 、 Format output

import numpy as np
print(np.pi)
# Format output {} Represents a placeholder , The results don't show , Internal parameters can be added 
print("pi value :{:.2f}".format(np.pi))

3.141592653589793
pi value :3.14

Two 、 The specified location

# example 2
print(" Big data technology terms :{}h and {}".format("Hadoop","Spark"))

Big data technology terms :Hadooph and Spark

# Appoint value Location 
print(" Big data technology terms :{1} and {0}".format("Hadoop","Spark"))

Big data technology terms :Spark and Hadoop

# Appoint value Specific value 
print(" Website name :{name}, website :{site}".format(name="Python",site="www.pyhton.org"))

Website name :Python, website :www.pyhton.org

3、 ... and 、 A few cases

1. All lowercase 、 All caps and initials are right name Format the output .

name=input()
print(name.lower())
print(name.upper())
print(name.title())

Input : niuNiu
Output :
niuniu
NIUNIU
Niuniu

2. Suppose the input is name by Niuniu, The output I am Niuniu and I am studying Python in Nowcoder! Please output the corresponding English sentence according to the above sentence pattern .

name=input()
print('I am {0} and I am studying Python in Nowcoder!'.format(name))

Input :
Niuniu
Output :
I am Niuniu and I am studying Python in Nowcoder!

3. Go to space :
Input description :
One string per line represents the name name( notes :name With some extra white space on both sides ).
Output description :
One line output name The original content after removing the blank characters on both sides .

name = input()
print(name.strip())

Input :
Niuniu
Output :
Niuniu


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved