python learning (3) about interactive input and string splicing

Input is an input statement for human-computer interaction. The input() function takes a standard input data and returns a string type. If you need to enter a non numeric value for, you need an additional definition.

sex=input("Sex:")   #In this case, it will default to Sex as a string type variable

#Need to be changed to:

sex=int(input("Sex:") #That's what makes Sex an integer

 

There are generally three ways of string splicing

1. Splicing with plus sign (the most intuitive method, but not recommended)

2. Splice with% placeholder

3. Use the. format formatter for space splicing.

See the following code specifically: program cases spliced with% placeholders

 1 name=input("name:")
 2 sex=input("sex:")
 3 age=int(input("age:"))   #age It's a plastic variable. It needs to use int()assignment
 4 
 5 infor='''
 6 ------infor of  %s-----
 7 name:%s
 8 sex:%s
 9 age:%d         #Because age is an integer variable, use% d
10 
11 '''%(name,name,sex,age)
12 
13 print(infor.title())       #.title The format is to capitalize the first letter of each line

Code run result:

 1 C:\Users\Administrator\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/Administrator/PycharmProjects/untitled/show530/interduce.py
 2 
 3 name:hongtao
 4 sex:male
 5 age:36
 6 
 7 ------Infor Of  Hongtao-----
 8 Name:Hongtao
 9 Sex:Male
10 Age:36
11 
12 
13 
14 Process finished with exit code 0

 

Program case of space splicing with. Format format tool:

1 name=input("Please input your name:")
2 sex=input("Please input your sex:")
3 job=input("Please input your job:")
4 saleary=int(input("Please input your saleary:"))
5 
6 
7 
8 print("Information of {_name}\n\tname:{_name}\n\tsex:{_sex}\n\tjob:{_job}\n\tsaleary:{_saleary}".format(_name=name,_sex=sex,_job=job,_saleary=saleary).title())

Code run result:

 1 C:\Users\Administrator\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/Administrator/PycharmProjects/untitled/show530/interduce2.py
 2 
 3 Please input your name:hongtao
 4 Please input your sex:male
 5 Please input your job:it
 6 Please input your saleary:30000
 7 
 8 
 9 Information Of Hongtao
10     Name:Hongtao
11     Sex:Male
12     Job:It
13     Saleary:30000
14 
15 
16 Process finished with exit code 0

Keywords: Python

Added by leapingfrog on Sun, 05 Apr 2020 20:09:06 +0300