Advanced use of container type (string)

Keywords: Python

;

Advanced use of container type (string)

  1. Splicing and repetition of strings
  2. Cross line splicing of strings
  3. Indexing and slicing of strings
  4. Built in function of string
  5. Escape of string
  6. Use of string and formatting and format built-in functions

Contents of this article

Splicing and repetition of strings

# 1. You can use + to splice multiple strings
res = 'hello' + ' ' + 'motherland'
print(res)

# 2. You can use * to repeat a string
res = 'hello ' * 3
print(res)

"""
result:
hello motherland
hello hello hello 
"""

Cross line splicing of strings

Next, we will learn a symbol in python, the cross line splicer \.

The function of this symbol is that if a line of code is too long, you can use this symbol to wrap lines, while the function of the original statement remains unchanged.

# If the variable char1 is defined without \, it is a syntax error.
char = 'hello'
char1 = \
	'hello'

# Cross line splicing of strings
# Use \ to splice strings in different lines, that is, when wrapping, you can use \ instead+
char = 'hello' + 'motherland'
char1 = 'hello' \
        'motherland'
# Of course, line breaks can still be used+
char2 = 'hello' + \
		'motherland'

Index of string

As we said before, the common feature of string, list and tuple containers is that they are orderly, accessible, and have positive and negative subscript indexes.

var = 'hello'
print(var[1])	# e

Slicing of strings

As the name suggests, slicing is to segment the string to obtain the required elements (slicing = = interception).

Syntax: string [start index: end index: interval value]

effect

From the element where the start index is located to the element before the end index according to the interval value, the element corresponding to the end index will not be obtained.

usage method
  1. [start index:] intercept from the start index to the last element. The interval value is 1 by default

    var = 'hello motherland'
    res = var[5:]
    print(repr(res))  # ' motherland'
    
  2. [: end index]: intercept the element from the first element to the one before the end index. The interval value is 1 by default

    var = 'hello motherland'
    res = var[:5]
    print(repr(res))  # 'hello'
    
  3. [start index: end index]: intercept the element from the start index to the end index. The interval value is 1 by default

    var = 'hello motherland'
    res = var[3:8]
    print(repr(res))  # 'lo mo'
    
  4. [start: end: interval]: the same as the third point, but intercepted according to the specified interval value

    var = 'hello motherland'
    
    # Intercept from specified location
    res = var[3:8:2]
    print(repr(res))  # 'l o'
    
    # Intercept from 0 by default
    res = var[:8:2]
    print(repr(res))  # 'hlom'
    
    # Reverse order interception
    res = var[::-1]
    print(repr(res))  # 'dnalrehtom olleh'
    
  5. [::], [:]: these two methods are to intercept all strings

    # Intercept all
    var = 'hello motherland'
    # Intercept from specified location
    res = var[:]
    print(repr(res))  # 'hello motherland'
    # Intercept from 0 by default
    res = var[::]
    print(repr(res))  # 'hello motherland'
    
    

Built in function of string

View the built-in function print(help(str)) of the string

functioneffect
capitalizetitle case
titleCapitalize the first letter of each word
upperAll letters are capitalized
lowerAll letters are lowercase
swapcaseCase interchange
countCalculate the number of a character
findFind the index position of the first occurrence of a string. If not found, return - 1
indexFind the index position of the first occurrence of a string. If it is not found, an error is reported
startswithDetermines whether to start with a string and returns a Boolean value
endswithDetermines whether to end with a string and returns a Boolean value
isupperDetermines whether all strings are capitalized and returns a Boolean value
islowerDetermines whether all strings are lowercase and returns a Boolean value
istitleDetermines whether each word in the string is capitalized
isalnumJudge whether the string is composed of numbers, letters and Chinese characters
isspaceDetermines whether a string is composed of only white space characters
isdecimalDetermines whether the string is composed of numbers and returns a Boolean value
ljustFill in the string, the original string is left, and a new string is returned
rjustFill in the string. The original string is on the right and returns a new string
centerFill the string, center the original string, and return a new string
stripRemove the blank characters on both sides of the beginning and end (the default is blank character, which can be specified)
lstripRemove the blank character on the left (the default is blank character, which can be specified)
rstripRemove the blank character on the right (the default is blank character, which can be specified)
splitSeparates strings into lists according to the specified characters
rsplitSeparates the string into a list from right to left according to the specified characters
joinConvert the container to a string according to a string
replaceReplace the characters in the string with other characters
formatFormatting of strings
capitalize
var = 'hello motherland'
res = var.capitalize()
print(res)		# Hello motherland
title
var = 'hello motherland'
res = var.title()
print(res)      # Hello Motherland
upper
var = 'hello motherland'
res = var.upper()
print(res)      # HELLO MOTHERLAND
lower
var = 'HELLO MOTHERLAND'
res = var.lower()
print(res)      # hello motherland
swapcase
var = 'Hello Motherland'
res = var.swapcase()
print(res)      # hELLO mOTHERLAND
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
find and index

Syntax: string.find(sub, [start,], [end])

Syntax: string.index(sub, [start,], [end])

# find and index services 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 and index reports 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 about what if the searched character itself is the last
var = 'Hello Motherland'
res = var.find('d')
print(res)      # 15
print(len(var))	# 16
Startswitch and endswitch

Syntax: startswitch (prefix, [start], [End])

Syntax: end switch (suffix, [start], [End])

var = 'Hello Motherland'

# See if the entire string starts with Hello
res = var.startswith('Hello')
print(res)      # True

# Check whether the string starts with Mother at index 6
res = var.startswith('Mother', 6)
print(res)      # True

# See if the entire string ends with aad
res = var.endswith('aad')
print(res)      # False
isupper and islower
var = 'Hello Motherland'

# Determines whether all strings are uppercase
res = var.isupper()
print(res)      # False

# Determines whether all strings are lowercase
res = var.islower()
print(res)      # False
isdecimal
var = '20666'
# Determine whether all strings are composed of numbers
res = var.isdecimal()
print(res)      # True
ljust,rjust,center

Syntax: string.ljust(width, [fillchar])

Specify a length. If the length of the string is not enough, it will be supplemented according to the specified string. By default, a space is used. The length of self delivery for supplementation cannot exceed 1.

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, '-')
print(res)          # -------Hello Motherland-------
strip,lstrip,rstrip
var = '     Hello Motherland      '
# Remove the strings on both sides
res = var.strip()
print(repr(res))    # 'Hello Motherland'

var = 'mmmmmmmmHello Motherlandmmmmmm '
# Remove the left
res = var.lstrip('m')
print(repr(res))    # 'Hello Motherlandmmmmmm'
# Remove the one on the right
res = var.rstrip('m')
print(repr(res))    # 'mmmmmmmmHello Motherlandmmmmmm '
# The rightmost part doesn't start with m, so it can't be removed
split and rsplit
var = 'Hello my motherland'

# By default, all are separated by spaces
res = var.split()
print(res)      # ['Hello', 'my', 'motherland']

# Specify the number of times to separate
res = var.split(' ', 1)
print(res)      # ['Hello', 'my motherland']

# Specify delimited characters
res = var.split('l')
print(res)      # ['He', '', 'o my mother', 'and']

# rsplit is separated from right to left
res = var.rsplit('l')
print(res)		# ['He', '', 'o my mother', 'and']

# Eh? How is the result of rsplit the same as that of rsplit? rspltd does not mean that the elements of the list are arranged from right to left, but that a character is found from the right of the string. If it is separated only once, we can see the difference in the results.

# rsplit is separated from right to left
res = var.rsplit('l', 1)
print(res)		# ['Hello my mother', 'and']
# split is separated from left to right
res = var.split('l', 1)
print(res)		# ['He', 'lo my motherland']

# See the difference?
join
lst = ['h', 'e', 'l', 'l', 'o']
res = '-'.join(lst)
print(res)      # h-e-l-l-o

string = 'hello'
res = '-'.join(string)
print(res)      # h-e-l-l-o
replace

Syntax: string.replace(old, new, [count])

var = 'hello hello my motherland'

# Replace 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

Escape of string

Use of escape characters

The escape character in python refers to \. Its function is to make the characters after this symbol meaningful and meaningless.

Meaningless characters refer to characters that are simply a string; Meaningful characters refer to characters that are not what you see on the surface, but have a special meaning.

Main escape characters
Symboleffect
\nLine feed (Unix or Linux)
\r\nLine feed (windows)
\tindent
\rReplace all characters before the line with all characters after the line
\bBackspace, delete 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)

# Backspace is used to delete a character
strvar = 'abcde\bfg'
print(strvar)   # abcdfg

There are some escape characters in some special path addresses, but we don't want these escape characters to be executed, so we can use prototype output.

# How to solve the problem when the path is escaped?
var = 'C:\Windows\twain_32'
print(var)		# C:\Windows	wain_32

# Method 1: use \ to make 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'

# Method 3: use metacharacters
'''
Add before character r Represents the prototype output of this string, and no escape characters in the string will be executed.
'''
var = r'C:\Windows\twain_32'
print(var)      # C:\Windows\twain_32

format string

Replace a character with a placeholder in the string, so that the character at that position can be replaced at will.

placeholder
  1. %d integer placeholder
  2. %f floating point placeholder
  3. %s string placeholder
Integer placeholder

You can fill in integer, decimal and Boolean values

# Integer can be filled in
var = 'I have%d Dollars' % (100)
print(var)      # I have 100 yuan

# You can also fill in decimals, but the display effect is integers
var = 'I have%d Dollars' % (100.99)
print(var)      # I have 100 yuan

# Fill in the Boolean value and convert it to the corresponding integer type
var = 'I have%d Dollars' % (True)
print(var)      # I have a dollar
Floating point placeholder

Like integers, you can fill in integer, decimal and Boolean values

# Decimal can be filled in
var = 'What is the displacement of my car%fT' % (2.0)
print(var)      # The displacement of my car is 2.000000 t

# You can also fill in integers, but the display effect is decimal
var = 'What is the displacement of my car%fT' % (2)
print(var)      # The displacement of my car is 2.000000 t

# It can be seen that there are too many decimal points
String placeholder

You can fill in any python legal type

# Decimal can be filled in
var = 'What is the displacement of my car%sT' % (2.0)
print(var)      # The displacement of my car is 2.0T

# You can also fill in integers, but the display effect is decimal
var = 'What is the displacement of my car%sT' % (2)
print(var)      # The displacement of my car is 2T

# You can also fill in integers, but the display effect is decimal
var = 'What is the displacement of my car%sT' % (True)
print(var)      # The displacement of my car is TrueT

# Use of multiple placeholders
var = 'My car is%s, Spent%d Million, accounting for% of my total assets%f%%' % ('BYD', 50, 0.000001)
print(var)	# My car is BYD, which cost 500000, accounting for 0.00000 1% of my total assets

# Note that when formatting a string, if you want to print a% separately, you need to enter two%%, so as to eliminate the placeholder meaning of%.

Use of format function

Format is also used to format strings, but it is more powerful than the above methods.

format uses braces instead of placeholders to pass values as its own parameters.

Syntax: 'string {} {}'. format(value1,value2)

Sequential parameter transmission

Parameters are passed one-to-one in the order of placeholders and values

# You can pass any data type. By default, it is a string placeholder.
var = '{} {}'.format('hello', 'motherland')
print(var)      # hello motherland
Index transfer parameter

Fill in 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
Keyword parameters

Prefix the parameter with a keyword, and then fill in the name of the keyword in square brackets. The keyword is transferred to the value according to the corresponding name.

var = '{msr} {world}'.format(msr='hello', world='motherland')
print(var)      # hello motherland
Container type parameters

If the parameter is data of a container type, the index value corresponding to the container can be filled in brackets for parameter transfer.

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 quotation marks
var = '{[0]} {[0]} {[one]}'.format(lst, tup, dit)
print(var)  # hello my motherland
Use of fill symbols

Fill symbols can be used to fill in strings that are not long enough

  • ^Center original string
  • >Original string right
  • < original string left

Syntax: {[keyword parameter]: [character to be filled] [original string position] < total character len gt h >}

Example:{who:*^10}
who : Keyword parameter, or subscript index
*   : Characters to fill(Default fill space)
^   : Position of the original string (left by default)
10  : Total character length = Original string length + Fill character length
var = '{price:-^20}'.format(price='Price')
print(var)	# ---------Price---------

# Note that the middle is indispensable
var = '{:*^10}'.format('Price')
print(var)	# ****Price****
Use of binary conversion symbols
  • : d integer placeholder
  • : f floating point placeholder
  • 😒 String placeholder
  • : money placeholder
# Integer placeholder
# It is required that the data type must be integer and cannot be compatible with any data type other than integer
var = 'My car{:d}ten thousand'.format(100)
print(var)  # My car is a million

# If there are digit requirements, add numbers; If there is a location requirement, fill symbols are used
strvar = 'I have{:^10d}Dollars'.format(100)
print(strvar)   # I have 100 yuan


# Floating point placeholder. The data type must be floating point
var = 'I use{:f}%My assets are used to pick up girls.'.format(100.00)
print(var)  # I use 100.000000% of my assets to pick up girls.

# We need to keep two decimal places and use. num
var = 'I use{:.2f}%My assets are used to pick up girls.'.format(100.00)
print(var)  # I use 100.00% of my assets to pick up girls.


# String placeholder. The data type must be string
var = 'Where is my house{:s}{:s}'.format('Beijing', 'Eighteen rings')
print(var)  # My house is on the 18th ring road of Beijing


# Money placeholder
#Separate a string of numbers by thousands
var = 'I have{:,}Yuan deposit'.format(100000000)
print(var)  # I have a deposit of 100000000 yuan

Posted by benn600 on Sun, 05 Sep 2021 01:05:38 -0700