Python string operation manual in practice

Source: Internet
Author: User

When using Python strings, we need to learn in detail. Next we will look at how to better use this string in specific operations. In later use, we still need to continue learning.

 
 
  1. # Coding: UTF-8
  2. # Two small Functions
  3. #1. Search for the position where the character first appears in the string.
  4. Def find (string, char ):
  5. Index = 0
  6. While index <len (string ):
  7. If (string [index] = char ):
  8. Return index
  9. Index + = 11
  10. Return-111
  11. #2. Search for the total number of characters in a string
  12. Def findSum (string, char ):
  13. Index = 0
  14. Count = 0
  15. While index <len (string ):
  16. If (string [index] = char ):
  17. Count + = 1
  18. Index + = 1
  19. Return count
  20. # Use the above two functions
  21. Print "position where character 1 appears for the first time in string 1211211234:", find ("1211
    211234 "," 1 ")
  22. Print "Number of times character 1 appears in string 1211211234:", findSum ("1211211
    234 "," 1 ")
  23. Import string # introduce the string Library
  24. Print string. find ('www .cctv.com ', 'com') # result = 9
  25. Print string. find ('good', 'D') # result = 3
  26. Print string. find ('Canada ', 'A',) # result = 3. The usage is as follows:
  27. # String. find (s, sub [, start [, end]) Function Description
  28. # Return the lowest index in s where the substring sub is
    Found such that sub is
  29. # Wholly contained in s [start: end]. Return-1 on failure.
    Defaults for start and
  30. # End and interpretation of negative values is the same
    As for slices.
  31. Print string. lowercase # constant, abcdefghijklmnopqrstuvwxyz
  32. Print string. uppercase # constant, ABCDEFGHIJKLMNOPQRSTUVWXYZ
  33. Print string. digits # constant, 012345678937
  34. Def isLower (char): # determines whether a character is lowercase.
  35. If (string. find (string. lowercase, char)>-1 ):
  36. Return 'good'
  37. Return 'bad'
  38. Print isLower ('s ')
  39. Def isLowertest (char): # Another method to determine whether the character is lowercase
  40. Return char in string. lowercase46 print isLowertest ('A ')

The above is an introduction to Python strings, and I hope you will get some benefits.

Related Article

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.