I'm sorry, everyone, I haven't updated for a long time due to the busy time. Today I finally have time to continue to learn python with you children's shoes.


As mentioned earlier, the definition of a string is to enclose some sequence of characters in quotation marks. E.g:

s = 'abc'

The execution process of the above line of code: first create a string object, and initialize the sequence of string values ​​'a', 'b', 'c', and then point the pointer s to this object. As shown below:

picture


Sequence Operations on Strings

We know that inside a string is a sequence of characters. As sequences, we can use subscripts to index . E.g:

c = s [ 0 ]     # c = 'a'

c = s [ 1 ]     # c = 'b'

also from character sequencesStart index at the end of , -1 for the last character, -2 for the second-to-last character, ... For example:

c = s [ -1 ]     # c = 'c'

c = s [ -2 ]     # c = 'b'

In addition, the string also supports the slice operation, which takes out the character sequence between the given two subscripts ( including the characters at the beginning of the subscript, but not the characters at the end of the subscript . The mathematical interval [a:b) is used to describe the is not clearer). E.g:

c = s [ 0 : 2 ]     # c = 'ab'

c = s [ 1 : 2 ]     # c = 'b'

c = s [ 1 : -1 ]     # c = 'b'

This is very similar to verilog's bit width operation, for example: reg s[7:0] = 8'h5a, then we can c = s[3:0] to extract 4'ha from 3 to 0 bits . It's just that verilog is closed interval.

When slicing, when the start subscript is 0, the subscript before the colon can be omitted; the end subscript points to the last element, and the subscript after the colon can also be omitted. So the slice above can be simplified to:

c = s [ : 2 ]     # c = 'ab'

c = s [ 1: ]     # c = 'bc'

When both the start and end subscripts are omitted, s [:] represents the entire character sequence 'abc'.

Pay attention to understand the difference between s[1:-1] and s[1:], the former does not include the last character, while the latter does.

As a sequence of characters, concatenation and repetition operations are also supported. E.g:

s1 = 'abc'

s2 = s1 + 'def'  # s2 = 'abcdef'

s3 = s1 * 3    # s3 = 'abcabcabc'

In addition, we have to pay attention to the read-only character sequence . We cannot assign values ​​to characters, s[0] = 'd' is illegal. But we can detour:

s = 'abc'        

s = 'd' + s [ 1 :]

But it should be noted that the second sentence does not simply modify the element value of the s object, but generates a new object by splicing, and makes s point to the new object.

String type operations

In addition to sequence operations, the string itself, as a class, comes with a lot of operation functions (methods of the class). The following table lists some common operations:

picture

index, find

position = index (substr, begin=0, end=len(string))

position = find (substr, begin=0, end=len(string))

Description: The index and find functions have the same function, both search for substrings. Start and end indices can be specified to search within a range.

Return value: The starting index value of the substring. The difference between index and find is that when no substring is found, index reports an error, while find returns -1.

E.g:

s = 'abcdefdef'

p1 = s.find ( ' de' ) # p1 = 3  

p2 = s . index ( 'de' , 5 )   # p2 = 6

replace

str_new = replace (substr_old, substr_new[, max])

Description : The replacement function, as its name suggests, finds the substring substr_old and replaces it with substr_new. The third parameter is optional and specifies the maximum number of replacements. The default is to replace all.

Return value : Returns the new string after replacement.

E.g:

s = 'abcdefdef'

s1 = s . place ( 'de', 'gh' )    # s1 = 'abchgfhgf'

s2 = s . place ( 'de' , 'gh' , 1 )    # s1 = 'abchgfedf'


split

list = split (str=' ', num)

Description : The split function splits a string into several substrings using the delimiter character str. num specifies how many times to split, if no number is specified, it will be split all.

Return value : The split list of substrings ( we'll learn about lists next time ).

E.g:

s = 'I am learning python'

list1 = s . split ( ' ' )    # list1 = ['I', 'am', 'learning', 'python']

list1 = s . split ( ' ', 2 )    # list1 = ['I', 'am', 'learning python']

upper, lower

str_new = upper()

str_new = lower()

Description : Convert the string to uppercase or lowercase.

Return value : The new string after case conversion.

E.g:

s = 'abc'

s1 = s.upper ( _ _)    # s1 = 'ABC'

s2 = s 1 . lower ()    # s2 = 'abc'

strip, lstrip, rstrip

str_new = strip (char=' ')

str_new = r strip (char=' ')

str_new = lstrip (char=' ')

Description: The strip function is used to remove the specified characters from the head or tail. The default is to remove spaces.

Return value: Returns the new processed string.

E.g:

s = 'abc\n'

s1 = s . lstrip ()    # s1 = 'abc\n'

s2 = s 1 . rstrip ('\n')    # s2 = 'abc'   

startswith, endswith

boolean = startswith (str, begin=0, end=len(string))

boolean = endswith (str, begin=0, end=len(string))

Description: Checks whether a string starts or ends with str, which can be checked within a specified range.

Return value: Returns True if checked, otherwise returns False.

E.g:

s = 'clk_a'

b1 = s . startswith ( 'clk' )    # b1 = True

s = 'rst_n'

b2 = s . endswith ( '_n' )    # s2 = True  

format

str_new = '{}{}...'. format (arg1, arg2, ...)

Description : format is used to format other numbers, strings, and even objects into strings. Braces {} are used to specify names, positions, formatting of numbers, etc.

Return value : The formatted new string.

E.g:

s = 'I am learning {lang}' . format ( lang = 'python' )   # s = 'I am learning python'

s = '{0} {1} {0}' . format ( 'face' , 'to' )   # s = 'face to face'

s = '{} {} {}' . format ( 'I' , 'love' , 'python' )   # s = 'I love python'

The first, replace by name.

The second is to replace by position.

The third, the default replacement by position, is also the most common replacement method.

Isn't it kind of like verilog's module instantiation? Either by name or by location.

formatNumber format

The rules for formatting numbers into strings are as follows:

picture

E.g:

s = "8'h{:0>2x}" . format ( 15)   # s = "8'h0f"

s = '{:.2%}' .format ( 3 / 9 )   # s = "33.33%"


There may be children's shoes to ask "there are so many functions built into the string, can't remember it?" So the following content is very important (knock on the blackboard).

1. How to see what other functions are built in string?

s = 'abc '

print ( dir ( s ))

dir() is a built-in function capable of viewing all properties and methods of a class. The result is as follows:

picture

2. How to check the usage of specific functions?

Python has taken this problem into account, no need for Baidu, no need to view the source code, just call the help() function. E.g:

print ( help ( s.find ) ) _

will print the following:

picture

Have you been moved by the intimate functions of python?

Next time we will learn python's lists.

--------------------

Welcome to pay attention to Ex ASIC . Share the experience and methods in the design of chip digital integrated circuits. Sharing makes work smoother.