Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The use of strings in the foundation of Python and what should be paid attention to

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Python foundation in the use of strings and what to pay attention to, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Why do I need a string?

When you call the browser to log in to some websites, you need to enter the password. After the browser sends the password to the server, the server will verify the password. The verification process is to compare the previously saved password with the password passed this time. If the password is equal, then the password is considered correct, otherwise it is considered wrong. Since the server wants to store these passwords, it can be implemented with a database (such as MySQL).

Of course, for simplicity's sake, we can first find a variable to store the password; so how to store the password with letters? A string is used at this point.

I. the format of the string in Python

The variable a defined below stores values of numeric types.

A = 100

The variable b, defined below, stores values of type string.

B = "hello itcast.cn" or b = 'hello itcast.cn'

Small summary:

The data in double or single quotation marks is a string

Second, string output

Example:

Name = 'ming' position =' lecturer 'address =' print ('-') print ("name:% s"% name) print ("position:% s"% position) print (1 / F, Jinyanlong Office Building, West Road, Building Materials City, Ping District, Zhongshan City "Company address:% s"% address) print ('- -')

Results:

-name: ming position: lecturer Company address: 1 / F, Jinyanlong Office Building, Building Materials City West Road, Changping District, Zhongshan City -III. String input

Through it, input can obtain data from the keyboard and save it to the specified variable.

Note: the data obtained by input is saved as a string, even if you enter a number.

Example:

UserName = input ('Please enter user name:') print ("user name:% s"% userName) password = input ('Please enter password:') print ("password:% s"% password)

Result: (the result varies according to the input)

4. Subscript and slice 1. Subscript index

The so-called "subscript" is the number, just like the number of the storage cabinet in the supermarket, through which the corresponding storage space can be found.

"subscript" in life

Supermarket lockers

The use of "subscript" in string

List and tuple support indexing is easy to understand, a string is actually an array of characters, so subscript indexes are also supported.

If there is a string: name = 'abcdef', the actual storage in memory is as follows:

If you want to take out some characters, you can use the subscript method (note that the subscript in Python starts at 0)

Name = 'abcdef' print (name [0]) print (name [1]) print (name [2])

Running result:

two。 Concept of slicing:

Slicing refers to the operation of intercepting a part of the object of operation. Strings, lists, and tuples all support slicing operations.

3. Syntax for slicing: [start: end: step]

Note: the selected interval belongs to the left-closed and right-open type, that is, from the "start" bit to the end of the "end" bit (excluding the end bit itself).

Let's take a string as an example.

If you take out part of it, you can use the following in brackets []:

Example:

Name = 'abcdef' print (name [0:3]) # take the character of subscript 0room2

Running result:

Example:

Name = 'abcdef' print (name [0:5]) # take the character with the subscript 0room4

Running result:

Example:

Name = 'abcdef' print (name [3:5]) # take characters with subscript 3 and 4

Running result:

Example:

Name = 'abcdef' print (name [2:]) # take the character with the subscript from 2 to the end

Running result:

Example:

Name = 'abcdef' print (name [1:-1]) # take the subscript between the beginning of 1 and the last second character

Running result:

> > a = "abcdef" > a [: 3] # running result 'abc' > > a [:: 2] # running result' ace' > > a [5:1:2]'# running result > > a [1:5:2] 'bd' # running result > a [::-2]' fdb' # running result > > a [5: 1 fdb' 2] 'fd' # running result 5. 16 common operations of string

Take the string 'lstr =' welcome to Beijing Museumitcpps fdsfs', as an example.

Describes common operations of characters.

Find

Detects whether the str is included in the lstr, and returns the starting index value, otherwise-1.

Syntax:

Lstr.find (str, start=0, end=len (lstr))

Example:

Lstr = 'welcome to Beijing Museumitcpps fdsfs' print (lstr.find ("Museum")) print (lstr.find ("dada"))

Running result:

Index

Just like the find () method, except that an exception is reported if str is not in lstr.

Syntax:

Lstr.index (str, start=0, end=len (lstr))

Example:

Lstr = 'welcome to Beijing Museumitcpps fdsfs' print (lstr.index ("dada"))

Running result:

Count

Returns the number of times str appears in lstr between start and end

Syntax:

Lstr.count (str, start=0, end=len (lstr))

Example:

Lstr = 'welcome to Beijing Museumitcpps fdsfs' print (lstr.count ("s"))

Running result:

Replace

Replace str1 in lstr with str2 and, if specified by count, no more than count times.

1str.replace (str1, str2, 1str.count (str1))

Example:

Lstr = 'welcome to Beijing Museumitcpps fdsfs' print (lstr.replace ("s", "ttennd"))

Running result:

Split

Slice lstr with str as the delimiter. If maxsplit has a specified value, only maxsplit substrings are separated

1str.split (str= "", 2)

Example:

Lstr = 'welcome to Beijing Museumitcpps fdsfs' print (lstr.split ("to", 5))

Running result:

Capitalize

Capitalize the first character of the string.

1str.capitalize ()

Example:

Lstr = 'welcome to Beijing Museumitcpps fdsfs' print (lstr.capitalize ())

Running result:

Title

Capitalize the first letter of each word in the string.

> a = "hello itcast" > a.title () 'Hello Itcast' # run result startswith

Check whether the string begins with obj. If so, return True. Otherwise, return False.

1str.startswith (obj)

Example:

Lstr = 'welcome to Beijing Museumitcpps fdsfs' print (lstr.startswith (' we'))

Running result:

Endswith

Check whether the string ends with obj, and if it returns True, it returns False.

1str.endswith (obj)

Example:

Lstr = 'welcome to Beijing Museumitcpps fdsfs' print (lstr.endswith (' hfs'))

Running result:

Lower

Convert all uppercase characters in lstr to lowercase

1str.lower ()

Example:

Lstr = 'welcome to Beijing Museumitcpps fdsfs' print (lstr.lower ())

Running result:

Upper

Convert lowercase letters in lstr to uppercase

1str.upper ()

Example:

Lstr = 'welcome to Beijing Museumitcpps fdsfs' print (lstr.upper ())

Running result:

Strip

Removes white space characters at both ends of the lstr string.

> a = "\ n\ t itcast\ t\ n" > a.strip () 'itcast' # run result rfind

It's similar to the find () function, except that it starts on the right.

1str.rfind (str, start=0,end=len (1str))

Example:

Lstr = 'welcome to Beijing Museumitcpps fdsfs' print (lstr.rfind (' eijing'))

Running result:

Rindex

Similar to index (), but starting on the right.

1str.rindex (str, start=0,end=len (1str))

Example:

Lstr = 'welcome to Beijing Museumitcpps fdsfs' print (lstr.rindex (' eijing'))

Running result:

Partition

The lstr is divided into three parts with str, before str, after str and str.

1str.partition (str)

Example:

Lstr = 'welcome to Beijing Museumitcpps fdsfs' print (lstr.partition (' eijing'))

Running result:

Join

Str is inserted after each character in the mystr to construct a new string.

Lstr = 'welcome to Beijing Museumitcpps fdsfs' str='233' lstr.join (str) li= ["my", "name", "is", "LY"] print (str.join (li))

Running result:

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report