Get the App
SLTechnology News&Howtos  ›  Development  › 

Case Analysis of Python data Type

Shulou Source: shulou.com Published: 2022-06-02 12:44:52 10月02日 Update

This article focuses on "Python data type case analysis", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "Python data type example analysis" it!

A summary of the content

String (str)

List (list)

Tuple (tup)

Dictionary (dict)

Data operation

II. String (str)

String manipulation in Python is basically similar to PHP. Let's introduce the general methods of string manipulation in Python.

3.1 Index subscript value

A character in a string can be obtained by indexing a subscript in PHP, as well as in Python, as shown in the following code

# definition string testStr = 'tangqingsong'# get the character print (testStr [1]) # print result a by index subscript

3.2 slicing

There is a very useful feature in Python called slicing. For example, when you want to get the interval characters from the nth character to the m character in a string, the slicing implementation using Python is very simple, as shown in the following code

# definition string testStr = 'tangqingsong'# gets the character print (testStr [0:4]) # print result tang by index subscript

3.3 cycle

In Python, for can loop not only arrays similar to those in PHP, but also strings, as shown in the following code

TestStr = 'tangqingsong'for value in testStr [0:4]: print (value) # print result tang

3.4 letter case conversion

The letter case conversion in Python is slightly different from the way PHP is written, and the operation in Python is similar to the feeling of PHP operands, as shown in the following code

# define string testStr = 'tQs'# uppercase conversion aStr = testStr.upper () # lowercase conversion bStr = testStr.lower () print (aStr,bStr) # print result TQS tqs

3.5 split and merge

Then let's take a look at how to cut and merge strings, as shown in the following code

S = 'tang | qing | song'l = s.split (' |') print (l) # print result ['tang',' qing', 'song'] S2 =' | '.join ([' tang', 'qing',' song']) print (S2) # print result tang | qing | song

3.6 string head and tail filtering

String head-to-tail filtering in PHP usually uses the trim () function filtering, while the syntax for string head-to-tail filtering in Python is somewhat similar to the way PHP manipulates objects, as shown below

# remove the content on the boundary strips1 = 'tang qing song' print (s1.strip ()) # print result' tang qing song'

If you want to specify a character to be deleted, the code looks like this

S2 =''print (s2.strip (' >')) # print result'

Tags: Data code character string result method Li Si Wang Wu type index dictionary array subscript loop content function variable Zhang San step size Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Microsoft Apple Huawei OPPO Reno