How to Write Python strings?

Source: Internet
Author: User

We need to learn a lot about using Python strings. In fact, no matter what the environment is, you need to master the relevant string application. Next, let's take a look at how Python strings are written.

 
 
  1. # Coding: UTF-8
  2. # String operations
  3. # Use brackets [] to extract any consecutive characters from a string
  4. # Note: The expression in brackets is a string index, which indicates the position of a character in the string,
  5. # The index of the first character of the string in the brackets is 0, not 1
  6. # Len returns the length of the string
  7. Test_string = "1234567890"
  8. Print test_string [0] # result = 1
  9. Print test_string [1] # result = 2
  10. Print test_string [9] # result = 0
  11. Print len (test_string) # result = 10
  12. # Use for loop to traverse strings
  13. For I in test_string:
  14. Print I
  15. If (I = '5 '):
  16. Print "Aha, I find it! "
  17. Print type (I) # <type 'str'> 20
  18. # Extract part of a string
  19. # The operator [n: m] returns part of the string. Starts from the nth string and ends with the nth string.
  20. # Contains n, but not m.
  21. # If n is ignored, the returned string starts from index 0.
  22. # If m is ignored, the string starts from n and ends with the last string.
  23. Test_string = 'abcdefghijklmnopqrstuvwxy'
  24. Print test_string [0: 5] # result = 'abcde'
  25. Print test_string [8: 11] # result = 'ijk'
  26. Print test_string [: 6] # result = 'abcdef'
  27. Print test_string [20:] # result = 'uvwxyz'
  28. Print test_string [:] # result = 'abcdefghijklmnopqrstuvwxy'

The above is an introduction to the code used by Python strings.

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.