SoFunction
Updated on 2024-11-16

Python strings in detail

synopsis

A sequence of strings is used to represent and store text. Strings in python are immutable and cannot be changed once declared.

Usually surrounded by single quotes (' ), double quotes (" ), triple quotes (''' """)

Where triple quotes can consist of multiple lines, the shortcut syntax for writing multi-line text, common language document strings, in specific locations in the document, is used as a comment. Convenient multi-line comments

Python actual three types of strings:

1. String in the usual sense of the word (str)
2. Raw strings, starting with uppercase R or lowercase r, r'', no escaping of special characters
String, u'' basestring subclasses
In Python, strings are "immutable sequences".

1. Immutable

2. Meet the basic operations of sequences, access by position, slicing and indexing.
string (computer science)
1. Access to help.

Copy Code The code is as follows.

>>> help(str)
>>> dir(str)
>>> help()

2. Immutability

Once created, they cannot be changed in place (as in java), and they cannot be changed by assigning a value to one of their positions; they are divided into immutable sequences, and the characters contained in these strings exist in left-to-right order and cannot be modified in place. strings in python are equivalent to lists of immutable sequences, and once they are declared, each character's position is fixed.

Meaning that if you want to change it, you have to build a new one!

Copy Code The code is as follows.

>>>s='spam'
>>>s[0]='k'   #TypeError
#Modify the string like java, reassign the value
s = ‘k' + s[1:]

original string

The original string constant, r "abcd", (r/R) i.e. the backslash escape mechanism is removed. Turn off the escape mechanism, i.e., \ no longer represents an escape

Usage:

1. Regular expressions

Used to process regular expressions and reduce backslashes

Copy Code The code is as follows.

p4search = (r'\s*')

2. System Path

The system path can be conveniently represented

Copy Code The code is as follows.

path = r'e:\book'

unicode string

Unicode is the standard method for writing international text.

Python allows you to work with Unicode text - you just prefix the string with u or U. For example, u "This is a Unicode string."

BP: Use Unicode strings when you work with text files, especially if you know that the file contains text written in a language other than English.

Common Operations

1.Basic operation

Copy Code The code is as follows.

+ :string1+string2 # concatenate the strings, linking the latter to the former.
Python does not allow other types in + expressions, you need to manually convert [this is different from java] 'abc'+str(9)
* :string*n #Create a new string repeating the original string n times
[] :string[n] #Get a character from the string at the corresponding position
[:] :string[n:m] #Intercept the string, if :m from the beginning to m if n: from n to the end.
in :char in string #Judge whether a character is in the string, if in return to true (True)
not in :char not in string # Judge whether a character is not in the string, if in return to true (True)
r/R : r/Rstring #Disable the actual meaning of the escaped character, the whole character is the original meaning.
len() : length len(s)

2. Type conversion

Convert strings and numbers to each other

String to number int/float/long

Number to string str

Copy Code The code is as follows.

>>> int(42)
42
>>> int('42')
42
>>> str(42)
'42'
>>> float('42.0')
42.0
>>> str(42.0)
'42.0'

Or use the string module's function

s: string to be converted, base: optional, target binary

Copy Code The code is as follows.

import string
(s[,base]) #base defaults to 10, if it is 0, then s can be a string of the form 012 or 0x23, if it is 16 then s can only be a string of the form 0x23 or 0X12

(s[,base]) # to long

(s[,base]) #convert to float

Conversion of strings and lists
String to list:

Copy Code The code is as follows.

s='spam'
l = list(s)

l2 = "hello world".spilt()

List to String

Copy Code The code is as follows.

k = ‘'.join(l)

Note that you can't join a non-string in a list

3. Modify the string

Copy Code The code is as follows.

s =  s + 'a'
s = s[3:] + ‘b'
s = (‘pl','pa')

a = '' #assign null

del a # Delete the entire variable

4. Indexing and slicing

Index s[i]

Copy Code The code is as follows.

s[0] first

s[-1] = s[len(s)-1] first from last


Segment s[i:j]
Copy Code The code is as follows.

Without the upper boundary, s[1:3] takes [1-2]
s[1:] take 1 to the end s[:3] take start to 2
Starting at s[:-1] to the penultimate
s[:] starts at the end, which is equivalent to a copy of the
s[1:10:2] take 1-9, step=2
s[a:b:-2] is a negative step, the meaning of the two boundaries is reversed, indicating that from b+1 to a, step -2
s='abcdefg'  
s[5:1:-1] gets fedc
s[1:3] == s[slice(1,3)] Built-in function

String Formatting

Only basic string formatting is covered here, and extensions are covered in subsequent pages %c Single characters %d Decimal integers %o Octal integers %s Strings %x Hexadecimal integers, where letters are lowercase %X Hexadecimal integers, where letters are uppercase

Copy Code The code is as follows.

>>> str = "so %s a day!"
>>> str % 'beautiful'
'so beautiful a day!'

>>> '{0} is {1}'.format('a','b')
'a is b'

>>> template = "{0}, {1} and {2}"
>>> ('a', 'b', 'c')
'a, b and c'

List of built-in functions
The string method is the number one python text processing tool.

()
Capitalize the first character of the string

(width,[,fill])
The original character is centered and spaces are filled to the length of the width.

(str,beg=0,end=len(string))
Get the number of substrings in a string, calculate the number of occurrences, you can specify the range

(encoding='UTF-8',errors='strict')
Decode the string, errors will be reported as ValueError by default, unless the errors are ignore or replace.

(encoding='UTF-8',errors='strict')
(suffix,beg=0,end=len(string))
Does it end in **

(tabsize=8)
Convert tabs to spaces in a string, default 8

(str,beg=0,end=len(stirng))
Checks if str is included, returns the start index if it is present, or -1 if it is not.

(str,begin=0,end=len(string))
The same as find, does not exist to report an exception, ValueError

()
At least one character, and all characters are letters or numbers, True. Detects if the string contains only 0-9A-Za-z.

()
At least one character, all characters are letters, True. Detects if the string contains only letters.

()
Contains only decimal numbers, True

()
Detects if the string contains only numbers, True.

()
At least one case-sensitive character and all characters are lowercase, True. Checks if the string is all lowercase.

()
Numeric characters only, True

()
Only contains spaces, True. Checks if the string is full of whitespace characters.

()
Detects if a word in a string is first letter capitalized.

()
At least one case-sensitive character and all characters are uppercase, True. Detects if the string is all uppercase.

(seq)
Using string as separator, merge all elements of seq into new string. Insert the original string between each of the two characters in the parameter string.

(width)
Returns an original string left-justified, with spaces added to the length width.

()
Convert to lowercase. Convert all strings to lowercase

()
Truncate the spaces on the left side

(str)
= find+split, from the first position where str appears, truncate to pre_str,str,after_str tuple, does not contain str then pre_str=(str1,str2,num=(str1)) replace, specify no more than num times, can be realized as a template

(str,beg=0,end=len(string))
Same as find, starting on the right

(str,beg=0,end=len(string))
Same as index, from the right.

(width)
Right Justify, Space Fill

(str)
Same partition, starting on the right.

([chars])
Clears right-hand whitespace, including newlines, and returns the processed string.

(str=””, maxsplit =(str))
Slices the string by str, specify the number of times to slice, slice the string, return a list, default delimiter is space.

(num=(‘\n'))
([keeps]) Separation by rows, with a specified number of splits

(obj,beg=0,end=len(string))
Starts with str, True. Checks if the string starts with a substring.

([obj])
Execute lstrip and rstrip on string


Reverse case in string. Lowercase to uppercase, uppercase to lowercase.

()
For title flowers, capitalize the first letter of the word, and lowercase the rest.

(str,del=””)
(table) according to str to give the table to convert string characters, to filter the characters in the del parameter

()
Convert to uppercase. Converts all strings to uppercase

(width)
Returns a string of length width, right-justified and padded with zeros.

len(string)
Get the length of the string

best practice

1. Use of length in loops

Copy Code The code is as follows.

while i < len(stri):
#Modify
size = len(stri)
while i < size

2. String append
Copy Code The code is as follows.

l = ['a', 'b']
result = ''
for i in l:
    result += i
#Modify
result = ''.join(l)

(sth. or sb) else
1. Escape character

A few common uses:

\n line breaks, \\\ backslashes
\t tabs \'single quotes
\r Carriage return \" double quotes

Follow-up needs to be expanded

String Encoding Explained
String Formatting
regular expression (math.)
Strings involve common modules (serialization/text wrapping, etc.)