Python learning Essay (ii)

Source: Internet
Author: User

Today is mainly user input and output, character stitching.

In Python, a multiline comment is 3 single quotation marks or double quotation marks that are preceded by a # number, as follows.

‘‘‘

Name = "Xiaoming"

Print (name)

‘‘‘

# name = "Xiaoming".

In Python, a single double quote function, unless it is a statement in the fear of single or double quotation marks too much, the characters are not clear, only back to the single double quotation marks, as follows.

Print ("I ' M xiaoming")

%s is a string, %d is a number, %f is a floating-point number, as follows:

Name = input ("Name:")

Age = Input ("Age")

Job = input ("job")

info = ""

-----Info of%s-----

name:%s

age:%s

job:%s

"% (Name,name,age,job)

Print (info)

Execute the above procedure

age:%s Change age:%d, execute the program

The error shows that%d input is not a str string, because the python default input is a string type,%d indicates that the character type received by age can only be integer type, causing the program to error.

Enter print (type age) under age = Input ("Age:") to see the type of character entered.

You can see that it is a character type, not an integer type, and you can change the age = Input ("Age:") to age = Int (input ("Age:")) and execute the program.

Here int is the conversion of input character type to integer, if you want to convert the integer type to a string, you need to change print (type (age)) to print (type (age), type (str))

You can see that the integer type has been converted to a string type.

The following is a two-segment code is the other two ways of character stitching

1>

Name = input ("Name:")
age = Int (input ("Age:"))
Print (Type (age), type (str))
Job = input ("Job:")
Salary = input ("Salary:")

info = ""
------info of {_name}-----
Name:{_name}
Age:{_age}
Job:{_job}
Salary:{_salary}
". Format (_name=name,
_age=age,
_job=job,
_salary=salary)
Print (info)
This is replaced by the _name instead of the variable name,_age age,_job instead of the job,_salary instead of salary

2>
Name = input ("Name:")
age = Int (input ("Age:"))
Print (Type (age), type (str))
Job = input ("Job:")
Salary = input ("Salary:")

info = ""
------info of {0}-----
NAME:{0}
Age:{1}
JOB:{2}
SALARY:{2}
". Format (name,age,job,salary)
Print (info)
Here is the substitution variable in order.
Today's share to the end of this, thank you!

Python learning Essay (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.