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.
- # Coding: UTF-8
- # Two small Functions
- #1. Search for the position where the character first appears in the string.
- Def find (string, char ):
- Index = 0
- While index <len (string ):
- If (string [index] = char ):
- Return index
- Index + = 11
- Return-111
- #2. Search for the total number of characters in a string
- Def findSum (string, char ):
- Index = 0
- Count = 0
- While index <len (string ):
- If (string [index] = char ):
- Count + = 1
- Index + = 1
- Return count
- # Use the above two functions
- Print "position where character 1 appears for the first time in string 1211211234:", find ("1211
211234 "," 1 ")
- Print "Number of times character 1 appears in string 1211211234:", findSum ("1211211
234 "," 1 ")
- Import string # introduce the string Library
- Print string. find ('www .cctv.com ', 'com') # result = 9
- Print string. find ('good', 'D') # result = 3
- Print string. find ('Canada ', 'A',) # result = 3. The usage is as follows:
- # String. find (s, sub [, start [, end]) Function Description
- # Return the lowest index in s where the substring sub is
Found such that sub is
- # Wholly contained in s [start: end]. Return-1 on failure.
Defaults for start and
- # End and interpretation of negative values is the same
As for slices.
- Print string. lowercase # constant, abcdefghijklmnopqrstuvwxyz
- Print string. uppercase # constant, ABCDEFGHIJKLMNOPQRSTUVWXYZ
- Print string. digits # constant, 012345678937
- Def isLower (char): # determines whether a character is lowercase.
- If (string. find (string. lowercase, char)>-1 ):
- Return 'good'
- Return 'bad'
- Print isLower ('s ')
- Def isLowertest (char): # Another method to determine whether the character is lowercase
- Return char in string. lowercase46 print isLowertest ('A ')
The above is an introduction to Python strings, and I hope you will get some benefits.