Advanced use of container types (strings)
- Concatenation and repetition of strings
- Interline concatenation of strings
- Indexes and slicing of strings
- Built-in functions for strings
- Escape of a string
- Use of strings and formatting and format built-in functions
This article directory
@[toc]
Concatenation and repetition of strings
# 1. You can concatenate multiple strings using +
res = 'hello' + ' ' + 'motherland'
print(res)
# 2. You can repeat a string with *
res = 'hello ' * 3
print(res)
"" results: Motherland hello hello hello hello ""
Copy the code
Interline concatenation of strings
Next we’ll look at a symbol in Python, the cross-line concatenator \.
The purpose of this symbol is that if a line of code is too long, this symbol can be used to break the line, leaving the original statement unchanged.
If the variable char1 is not defined \ it is a syntax error.
char = 'hello'
char1 = \
'hello'
# Interline concatenation of strings
# uses \ to concatenate strings on different lines, meaning you can use \ instead of + when wrapping a line
char = 'hello' + 'motherland'
char1 = 'hello' \
'motherland'
Of course, line breaks can still use +
char2 = 'hello' + \
'motherland'
Copy the code
Index of string
As we said earlier, strings and lists and tuples have in common ordered, reachable, and forward-backward subscript indexes.
var = 'hello'
print(var[1]) # e
Copy the code
Slicing of strings
Slicing, as the name implies, is the cutting of a string to obtain the desired elements (slicing == intercept).
Syntax: string[Start index: End index: interval value]
role
The interval value from the element where the start index is located to the element before the end index is truncated. The element corresponding to the end index will not be retrieved.
Method of use
-
[Start index :] : intercepts from the start index to the last element. The default interval value is 1
var = 'hello motherland' res = var[5:] print(repr(res)) # ' motherland' Copy the code
-
[: end index] : intercepts elements from the first element to the one before the end index. The default interval value is 1
var = 'hello motherland' res = var[:5] print(repr(res)) # 'hello' Copy the code
-
[Start index: end index] : Intercepts the element before the start index to the end index. The default interval value is 1
var = 'hello motherland' res = var[3:8] print(repr(res)) # 'lo mo' Copy the code
-
[Start: End: interval] : Same as the third point, but truncated by the specified interval value
var = 'hello motherland' # intercept from the specified position res = var[3:8:2] print(repr(res)) # 'l o' Interception starts at 0 by default res = var[:8:2] print(repr(res)) # 'hlom' # reverse order interception res = var[::-1] print(repr(res)) # 'dnalrehtom olleh' Copy the code
-
[::] and [:] : intercepts all strings
# Intercept all var = 'hello motherland' # intercept from the specified position res = var[:] print(repr(res)) # 'hello motherland' Interception starts at 0 by default res = var[::] print(repr(res)) # 'hello motherland' Copy the code
Built-in functions for strings
Look at the built-in function print(help(STR)) for strings
function | role |
---|---|
capitalize | Uppercase |
title | Capitalize the first letter of each word |
upper | All letters in capitals |
lower | All lowercase letters |
swapcase | Case interchange |
count | Count the number of a character |
find | Returns -1 if the first occurrence of a string is not found |
index | Failed to find the index of the first occurrence of a string |
startswith | Checks whether it starts with a string and returns a Boolean value |
endswith | Checks whether it ends in a string and returns a Boolean value |
isupper | Checks if all strings are uppercase and returns a Boolean |
islower | Checks if all strings are lowercase and returns a Boolean value |
istitle | Determines if each word of the string is capitalized |
isalnum | Check whether the string consists of numbers, letters, and Chinese characters |
isspace | Determines if the string consists of whitespace only |
isdecimal | Checks if the string is composed of numbers, returning a Boolean value |
ljust | Padding string, the original string to the left, returns the new string |
rjust | Padding string, the original string to the right, returns the new string |
center | Fills the string, the original string is centered, and returns the new string |
strip | Remove whitespace at both ends (default whitespace, can be specified) |
lstrip | Remove the left whitespace, (default whitespace, can be specified) |
rstrip | Remove the whitespace on the right, (default whitespace, can be specified) |
split | Separates a string into a list of specified characters |
rsplit | Separates a string from right to left into a list of specified characters |
join | Convert a container to a string as a string |
replace | Replaces characters in a string with other characters |
format | String formatting |
capitalize
var = 'hello motherland'
res = var.capitalize()
print(res) # Hello motherland
Copy the code
title
var = 'hello motherland'
res = var.title()
print(res) # Hello Motherland
Copy the code
upper
var = 'hello motherland'
res = var.upper()
print(res) # HELLO MOTHERLAND
Copy the code
lower
var = 'HELLO MOTHERLAND'
res = var.lower()
print(res) # hello motherland
Copy the code
swapcase
var = 'Hello Motherland'
res = var.swapcase()
print(res) # hELLO mOTHERLAND
Copy the code
count
Syntax: String.count (sub, [start,], [end])
String.count (string, [start value index], [end value index])
# Note that count is case-sensitive
var = 'Hello Motherland'
res = var.count('h')
print(res) # 1
res = var.count('H'.3.10)
print(res) # 1
Copy the code
Find and index
Syntax: String. find(sub, [start,], [end])
Syntax: String.index (sub, [start,], [end])
# find and index service case
var = 'Hello Motherland'
res = var.find('h')
print(res) # 9
res = var.index('h')
print(res) # 9
If the character cannot be found, find returns -1, index returns an error
res = var.find('m'.3)
print(res) # 1
res = var.index('m'.3)
print(res) # error
# find will only return the forward index, so don't worry what if the character searched for is itself the last one
var = 'Hello Motherland'
res = var.find('d')
print(res) # 15
print(len(var)) # 16
Copy the code
Startswith and endswith
Syntax: startswith(prefix, [start], [end])
Endswith (suffix, [start], [end])
var = 'Hello Motherland'
# check if the entire string begins with Hello
res = var.startswith('Hello')
print(res) # True
Check if the string starts with Mother at index 6
res = var.startswith('Mother'.6)
print(res) # True
Check if the entire string ends in aAD
res = var.endswith('aad')
print(res) # False
Copy the code
Isupper and islower
var = 'Hello Motherland'
Check if all strings are uppercase
res = var.isupper()
print(res) # False
Check if all strings are lowercase
res = var.islower()
print(res) # False
Copy the code
isdecimal
var = '20666'
# check whether the string is composed of numbers
res = var.isdecimal()
print(res) # True
Copy the code
Ljust, rjust, center
Syntax: string.ljust(width, [fillchar])
Specify a length. If the length of the string is insufficient, the specified string is supplemented. The default value is space.
var = 'Hello Motherland'
res = var.ljust(20)
print(repr(res)) # 'Hello Motherland '
res = var.rjust(30.'m')
print(res) # mmmmmmmmmmmmmmHello Motherland
print(len(res)) # 30
res = var.center(30.The '-')
print(res) # -------Hello Motherland-------
Copy the code
Strip, Lstrip, Rstrip
var = ' Hello Motherland '
# Remove strings at the beginning and end
res = var.strip()
print(repr(res)) # 'Hello Motherland'
var = 'mmmmmmmmHello Motherlandmmmmmm '
# remove the left side
res = var.lstrip('m')
print(repr(res)) # 'Hello Motherlandmmmmmm'
# Remove the one on the right
res = var.rstrip('m')
print(repr(res)) # 'mmmmmmmmHello Motherlandmmmmmm '
# m # m # m # m # m # m
Copy the code
The split and rsplit
var = 'Hello my motherland'
# by default, space is used to delimit all
res = var.split()
print(res) # ['Hello', 'my', 'motherland']
# specify the number of separations
res = var.split(' '.1)
print(res) # ['Hello', 'my motherland']
# Specifies the delimited characters
res = var.split('l')
print(res) # ['He', '', 'o my mother', 'and']
# rsplit split from right to left
res = var.rsplit('l')
print(res) # ['He', '', 'o my mother', 'and']
# yi? How can rsplit be the same as rsplit? RSPLTD does not mean that the list of elements is arranged from right to left, it means that we start from the right side of the string to find a character, if we just split it once we can see the difference.
# rsplit split from right to left
res = var.rsplit('l'.1)
print(res) # ['Hello my mother', 'and']
# split split from left to right
res = var.split('l'.1)
print(res) # ['He', 'lo my motherland']
# See the difference?
Copy the code
join
lst = ['h'.'e'.'l'.'l'.'o']
res = The '-'.join(lst)
print(res) # h-e-l-l-o
string = 'hello'
res = The '-'.join(string)
print(res) # h-e-l-l-o
Copy the code
replace
Syntax: String.replace (old, new, [count])
var = 'hello hello my motherland'
# replace the characters
res = var.replace('hello'.'hello')
print(res) Hello, my motherland
# Replace one of the characters
res = var.replace('hello'.'hi'.1)
print(res) # hi hello my motherland
Copy the code
Escape of a string
Use of escape characters
The escape character in Python refers to \, and its function is to make the characters that follow the symbol meaningful and meaningless.
A nonsense character is a character that is simply a string; Meaningful characters are not the characters you see on the surface, but the characters that have a special layer of meaning.
The main escape character
symbol | role |
---|---|
\n | Newline (Unix or Linux) |
\r\n | Line wrap (Windows) |
\t | The indentation |
\r | Replaces all characters following the line with all characters preceding the line |
\b | Backspace deletes a character |
var = 'hello\nmotherland'
print(var)
print()
var = 'hello\r\nmotherland'
print(var)
print()
var = 'hello\tmotherland'
print(var)
print()
var = 'hello\rmotherland'
print(var)
Copy the code
The backspace character is used to delete a character
strvar = 'abcde\bfg'
print(strvar) # abcdfg
Copy the code
There are some special path addresses that have some escape characters, but we don’t want these escape characters to be executed, so we can use prototype output.
The path is escaped. How can I solve this problem?
var = 'C:\Windows\twain_32'
print(var) # C:\Windows wain_32
# method 1: use \ to render escape characters meaningless
var = 'C:\Windows\\twain_32'
print(var) # C:\Windows\twain_32
# Method 2: Use the repr function to prototype the output
var = 'C:\Windows\twain_32'
res = repr(var)
print(res) # 'C:\\Windows\twain_32'
Use metacharacters
"" Preceded by r indicates that this string is prototyped, and no escaping characters in the string are executed. ' ' '
var = r'C:\Windows\twain_32'
print(var) # C:\Windows\twain_32
Copy the code
Formatted string
To replace a character with a placeholder in a string so that characters at that position can be replaced at will.
A placeholder
- %d integer placeholder
- %f floating point placeholder
- %s string placeholder
Integer placeholder
Can fill in integer, decimal, Boolean value
Can be an integer
var = 'I have % D dollars.' % (100)
print(var) I have 100 yuan
# can also be filled in as a decimal, but the display effect is an integer
var = 'I have % D dollars.' % (100.99)
print(var) I have 100 yuan
Enter a Boolean value and convert it to the corresponding integer type
var = 'I have % D dollars.' % (True)
print(var) I have 1 dollar
Copy the code
Floating point placeholders
Like integers, integers, decimals, and Booleans can be filled in
You can fill in decimals
var = 'The displacement of my car is %fT.' % (2.0)
print(var) # The displacement of my car is 2.000000 TONS
# Can also be filled in as an integer, but will appear as a decimal
var = 'The displacement of my car is %fT.' % (2)
print(var) # The displacement of my car is 2.000000 TONS
As you can see, there are too many decimal points
Copy the code
String placeholder
You can fill in any python legal type
You can fill in decimals
var = 'The displacement of my car is %sT' % (2.0)
print(var) # The displacement of my car is 2.0T
# Can also be filled in as an integer, but will appear as a decimal
var = 'The displacement of my car is %sT' % (2)
print(var) My car has a displacement of 2T
# Can also be filled in as an integer, but will appear as a decimal
var = 'The displacement of my car is %sT' % (True)
print(var) # My car's displacement is TrueT
# Use of multiple placeholders
var = 'My car is % S, and it cost % D, which is % F %% of my total assets.' % ('比亚迪'.50.0.000001)
print(var) # My car is BYD, which cost 500,000 yuan, accounting for 0.000001% of my total assets
Note that when formatting a string, if you want to print a separate %, you need to type two %% to eliminate the placeholder meaning of %.
Copy the code
Use of the format function
Format is also used to format strings, but it is more powerful than the above method.
Format uses curly braces instead of placeholders to pass in values as its own parameters.
Grammar: ‘string {} {}’. The format (value1, value2)
Order the participation
Parameters are passed one-to-one in the order of placeholders and values
# Any data type can be passed. The default is a string placeholder.
var = '{} {}'.format('hello'.'motherland')
print(var) # hello motherland
Copy the code
Index and parameter
Enter the index value of the format parameter in brackets to pass the parameter
# Reverse index is not supported
var = '{1} {0}'.format('hello'.'motherland')
print(var) # motherland hello
Copy the code
Parameter transmission by keyword
Prefix a parameter with a keyword, and then fill in the name of the keyword in brackets. The keyword is passed based on the name.
var = '{msr} {world}'.format(msr='hello', world='motherland')
print(var) # hello motherland
Copy the code
The container type is passed
If the parameter is data of a container type, you can pass the parameter by filling the index value of the container in brackets.
lst = ['hello'.'goodbye']
tup = ('my'.'your')
dit = {'one': 'motherland'.'two': 'world'}
Do not specify elements in the container
var = '{} {} {}'.format(lst, tup, dit)
print(var)
# ['hello', 'goodbye'] ('my', 'your') {'one': 'motherland', 'two': 'world'}
# specify element
# dictionary keys do not need quotes
var = '{[0]} {[0]} {[one]}'.format(lst, tup, dit)
print(var) # hello my motherland
Copy the code
Use of padding symbols
You can use padding symbols to fill in strings that are not long enough
- ^ The original string is centered
- > The original string is to the right
- < The original string is left
Syntax: {[keyword argument]:[character to fill][original string position]< total character length >}
Example: {who:*^10} who: keyword parameter or index * : character to be filled (default: blank) ^ : position of original string (default: left) 10: Total character length = Original string length + padding character lengthCopy the code
var = '{price:-^20}'.format(price='price')
print(var) # --------- price ---------
# Notice, in the middle: indispensable
var = '{: * ^ 10}'.format('price')
print(var) # **** price ****
Copy the code
The use of base conversion symbols
- :d integer placeholder
- :f floating point placeholder
- :s String placeholder
- :, money placeholder
# Integer placeholder
The datatype must be an integer, not compatible with any other datatype
var = 'My car {:d} ten thousand'.format(100)
print(var) # MyCar 1 million
Add number if required; Use a fill symbol if a position requires it
strvar = 'I have {:^10d} dollars'.format(100)
print(strvar) I have 100 yuan
The data type must be a floating point placeholder
var = 'I use {:f}% of my assets to pick up women. '.format(100.00)
print(var) # I spend 100.000000% of my assets on girls.
We need to reserve two decimal places, using.num
var = 'I use {:.2f}% of my assets to pick up women. '.format(100.00)
print(var) # I spend 100.00% of my assets on girls.
String placeholder that requires the data type to be a string
var = 'My house is in {:s}{:s}'.format('Beijing'.'Eighteen rings')
print(var) My house is on the 18th Ring Road in Beijing
# money placeholder
# Separate a string of numbers by thousands
var = 'I have {:,} yuan in my account'.format(100000000)
print(var) I have a deposit of 100,000,000 yuan
Copy the code