Several methods of Python string concatenation

Source: Internet
Author: User
Tags python string concatenation

At that time, after reading the basic syntax of Python to a friend wrote a dollar probability conversion after the stitching results found that do not know how Python splicing strings to see some of the information they made a summary

The first is the same way that JavaScript is stitched together.

Name = input ("Name:") Age= Input ('Age :') Job= Input ('Job:') Str="""------string concatenation------"""+"""Name"""+name+"""Age"""+age+"""Job"""+jobprint (str)

Multi-memory is not recommended via plus stitching

The second is through placeholder stitching recommendations

Name = input ("name:"= input ('age:'= input ( ' Job: '  "'    -------string concatenation  -------    Name:% s Age    :%D    Job:%D' ' % (name,int (age), int (Job))print (str)

The%s in the string represents the character type%d, which means that the number type is in% () passed in must type corresponds or the other type is not more descriptive.

The third way to call the Format method by string is to recommend

Name = input ("name:"= input ('age:'= input ( ' Job: '  "'    -------info of {_name}-------    name:{_name}    age:{_age}    job:{_ Job}'  . Format (_name = name            , _age = Age,_job = job) print (str)

The result is the same as above

A variable defined within a string is assigned to it in format.

Format can also be used to recommend

Name = input ("Name:") Age= Input ('Age :') Job= Input ('Job:') STR3=" "-------Hello Word {0} -------name:{0} age:{1} job:{2}" ". Format (name,age,job)print (STR3)

It is necessary to use the order to get the subscript to be passed.

About string concatenation that's it. Friendship Reminder in Python forget the plus sign splicing very low performance

Several methods of Python string concatenation

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.