Python String Connection Operation summary

Source: Internet
Author: User

" "python string connection operation" "#Method 1 via + connectionA, B ='Hello','Hadoop'Print(A +b)#Method 2 through, connectPrint(A, b)#assignment operation will narimoto group#Method 3 Direct connection, there are no spaces canPrint('Hadoop' 'Hive')Print('Hadoop"'Hive')#Method 4 using the% operatorPrint('%s%s'% ('Hello',' World'))Print('%s%s'% ('Hadoop','Java'))#Format between% determines the format of the output resultPrint('%s\n%s'% ('Hadoop','Java'))Print('%s\t%s'% ('Hadoop','Java'))#Method 5 Format MethodsPrint('{}{}'. Format ('Hello',' World'))Print('{}\t{}'. Format ('Hello',' World'))#Format between {} determines the format of the output resultPrint('{}\n{}'. Format ('Hello',' World'))" "Method 6 The join string has a built-in methods join whose arguments are a sequence type, such as an array or a tuple. " "Print('='. Join (['a','b','DD']))#The output format determines for itself#Method 7 Using the F-string method" "' Python 3.6 introduces the formatted string literals (literal formatting string), abbreviated f-string,f-string is the evolution version of the% operator and the format method, using the F-string connection string method Similar to using the% operator, format method. " "Print(f'{a}{b}')Print(f'{A} {B}')#The output format determines for itselfPrint(f'{a}+{b}')Print(f'{a}-{b}')Print(f'{a}=>{b}')#Method 7 * operator ConnectionPrint(a*3)#* operator is actually an operator overloaded operation, the corresponding Magic method is __mul__" "Summary: When connecting a small number of strings, it is recommended to use the + sign operator. If you have high performance requirements, and the Python version is above 3.6, we recommend using F-string. For example, the following f-string readability is much better than the + sign:" "name='Hadoop' Age= 12Gender='Femal'a= f'name: {name} Age: {ages} Gender: {gender}'b='Name:'+ name +'\ t Age:'+ STR (age) +'\ t Sex:'+GenderPrint(a)Print(b)" "Joins and f-string are recommended when connecting a large number of strings, and depending on the version of Python you use and the readability requirements you choose. " "

Python String Connection Operation summary

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.