Three ways to concatenate strings in Python

Source: Internet
Author: User

1. Connect ' AA ' BB between strings

Can be a space or nothing at the center.

Then the output is closely connected to each other.

2. String + number

' AA ' +90

This will cause an error, because different types cannot be added,

can be used ' AA ', 90. This is possible, but there is a space in the middle.

3,% placeholder

print '%s,%s '% (' Tom ', ' Jerry ')

[Python]View PlainCopy
  1. >>> Print ' aaa ',
  2. AAA
  3. >>> print ' AA '
  4. Syntaxerror:invalid syntax
  5. >>> print ' AA ' +
  6. Traceback (most recent):
  7. File "<pyshell#5>", line 1, in <module>
  8. print ' AA ' +
  9. Typeerror:cannot concatenate ' str ' and ' int ' objects
  10. >>> print ' AA ' +' CAA '
  11. Aacaa
  12. >>> print ' aa ' bb '
  13. Aabb
  14. >>> print ' AA 'rr '
  15. Aarr
  16. >>> print ' AA ',
  17. AA
  18. >>> print '%s,%s '% (' Tom ',' Jerry ')
  19. Tom,jerry
  20. >>> '%s,%d '% (' Tom ', ' a ')
  21. ' Tom,56 '
  22. >>>

Three ways to concatenate strings in Python

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.