String output and input

String output

demo

name = ' itheima ' 
position = ' lecturer ' 
address = ' Beijing ' 

print ( ' ------------------------------- ------------------- ' )
 print ( " Name:% s " % name)
 print ( " Job:% s " % position)
 print ( " Company address:% s " % address)
 print ( ' ------------------------------------------ -------- ')

result:

-------------------------------------------------- 
Name: itheima 
Position: Lecturer 
Company address: Beijing
 --------------------------------------- -----------

String input

Before learning input, it can be used to obtain data from the keyboard and then save it to the specified variable;

Note: The data obtained by input is saved as a string, even if the input is a number, it is also saved as a string

demo:

userName = input ( ' Please enter user name: ' )
 print ( " User name:% s " % userName) 

password = input ( ' Please enter password: ' )
 print ( " Password is:% s " % password)

Results: (The results are different depending on the input)

Please enter username: itheima 
username: itheima 
please enter password: haohaoxuexitiantianxiangshang 
password is: haohaoxuexitiantianxiangshang

example

# print or input can add any data type 
# print () 
# input () 

# # variable 
name = " 名字" 
# # Please enter your name 
ret1 = " Please enter your% s " % name 
my_name = input (ret1)

operation result:

Please enter your name
# # My age is 18 years old 
Age = 18 # Please get a string -> My age is 18 years old 
# 01: Format Symbol 
RET2 = " My age is the age of% d " % Age
 # 02: addition operator - > Merging of strings 
ret3 = " My age is " + str (age) + " year " print (ret3)


operation result:

My age is 18 years old

 

Guess you like

Origin www.cnblogs.com/kangwenju/p/12688941.html