Python - Length and Split Function

Python Exercise Lists

How to get string length or number of bytes in Python

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:


Where string is used to specify the string in order to count its length.

For example, to define a string with the content "” and then use the len() function to calculate the length of the string, the execution code is as follows:

a = ""

The output is:


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.

How to split string using Python?

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:

  • str: the string to be split;
  • • sep: used to specify the separator, which can contain multiple characters. This parameter defaults to None, which means all empty characters, including spaces, newline characters "\ n", tab characters "\ t", and so on.
  • maxsplit: An optional parameter used to specify the number of splits. The maximum number of substrings in the last list is maxsplit + 1. If not specified or specified as -1, there is no limit to the number of splits.

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.

#Split with default separator
list_1 = string_id.split ()
#Split with multiple characters
list_2 = string_id.split ('>>>')
#Split with .
list_3 = string_id.split ('.')
#Split with spaces and stipulate that it can only be split into 4 substrings at most
list_4 = string_id.split ('', 4)
#Split using> characters
list_5 = string_id.split ('>')

The output is:

['PYTHON', 'LANGUAGE', 'WEBSITE', '>>>', '']
['PYTHON LANGUAGE WEBSITE >>> www', 'freelearningpoints', 'com']
['PYTHON', 'LANGUAGE', 'WEBSITE', '>>>', '']

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:

list_6 = string_id.split () #contains 3 consecutive spaces

The output is:

['PYTHON', 'LANGUAGE', 'WEBSITE', '>>>', '']


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression

More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises