In Python, to know how many characters a string has (getting the string length), or how many bytes a string occupies, you can use the len function.
The basic syntax of the len function is:
len(string)
Where string is used to specify the string in order to count its length.
For example, to define a string with the content "www.freelearningpoints.com” and then use the len() function to calculate the length of the string, the execution code is as follows:
a = "www.freelearningpoints.com" len(a)
The output is:
26
In actual development, in addition to often obtaining the length of the string, sometimes also the number of bytes of the string. In Python, different characters occupy different numbers of bytes. Numbers, English letters, decimal points, underscores, and spaces each occupy one byte.
In Python, in addition to using some built-in functions to get information about strings (such as the len() function to get the length of a string), the string type itself also has some methods for us to use.
Note that the method mentioned here refers to the string type str itself. Because it involves knowledge of classes and objects, beginners do not need to dive deeply into it, they only need to know the specific usage of the method.
Starting from this section, we will introduce some common string type methods. This section first introduces the split () method of splitting a string.
The split () method can be used to split a string into multiple substrings according to the specified delimiter. These substrings will be saved in the list (excluding the delimiter) and returned as the method's return value. The basic syntax of the method is as follows:
str.split(sep, maxsplit)
The meanings of the parameters in this method are:
In the split method, you cannot specify the maxsplit parameter if you do not specify the sep parameter.
Different from the use of built-in functions (such as len), methods owned by string variables can only be called by "string.method name()". There is no need to confuse why here, after learning classes and objects, you will naturally understand.
For example, define a string that holds the website of this Python language, and then use the split() method to separate them according to different separators.
string_id = "PYTHON LANGUAGE WEBSITE >>> www.freelearningpoints.com" print(string_id) #Split with default separator list_1 = string_id.split () print(list_1) #Split with multiple characters list_2 = string_id.split ('>>>') print(list_2) #Split with . list_3 = string_id.split ('.') print(list_3) #Split with spaces and stipulate that it can only be split into 4 substrings at most list_4 = string_id.split ('', 4) print(list_4) #Split using> characters list_5 = string_id.split ('>') print(list_5)
The output is:
PYTHON LANGUAGE WEBSITE >>> www.freelearningpoints.com
['PYTHON', 'LANGUAGE', 'WEBSITE', '>>>', 'www.freelearningpoints.com']
['PYTHON LANGUAGE WEBSITE', 'www.freelearningpoints.com']
['PYTHON LANGUAGE WEBSITE >>> www', 'freelearningpoints', 'com']
['PYTHON', 'LANGUAGE', 'WEBSITE', '>>>', 'www.freelearningpoints.com']
['PYTHON LANGUAGE WEBSITE', '', '', 'www.freelearningpoints.com']
It should be noted that when the sep parameter is not specified, the split () method uses empty characters to divide by default, but when there are consecutive spaces or other empty characters in the string, it will be regarded as a separator to divide the string. Example:
string_id = "PYTHON LANGUAGE WEBSITE >>> www.freelearningpoints.com" list_6 = string_id.split () #contains 3 consecutive spaces print(list_6)
The output is:
['PYTHON', 'LANGUAGE', 'WEBSITE', '>>>', 'www.freelearningpoints.com']
More Tutorials:
Python Installation - Linux (Ubuntu)More Python Exercises:
Python String Exercises