Data types in Python: strings

Posted by The Swedish Tower on Mon, 04 Oct 2021 22:43:40 +0200

        In Python programs, string type is the most common data type. You can use quotation marks (single quotation marks or double quotation marks) to create strings. Creating Python strings is very simple, as long as you assign a value to a variable. As follows:

var1 = 'Hello World'        #String type variable
var2 = "Python R"            #String type variable

        In Python programs, strings are usually composed of single quotation marks' ', double quotation marks'', three single quotation marks or a string surrounded by three double quotation marks.

        1. A single quote string is essentially the same as a double quote string. However, if a single quote string is used after a single quote is contained in the string, it will be impossible to distinguish the single quote in the string from the single quote of the string flag. Therefore, an escape string should be used. If a double quote string is used, the single quote can be directly calculated in the string. For example:

'abc"dd"ef'
"'abc'd'12"

        2. A three quotation mark string can be composed of multiple lines, but not a single quotation mark or double quotation mark string. It can be used when a long and multi line string needs to be used. For example:

'''
This is the string
'''

        The characters in the string can also contain numbers, letters, Chinese characters, special symbols, and some invisible control characters, such as line feed, tab, etc.

1.1 accessing the value of a string

        String is a string that can be retrieved by sequence number (sequence number starts from 0), for example:

var1 = 'Hello World'
var2 = "Python Toppr"
print("var1[0]", var1[0])      #Intercept the first character in the first string and output: var1[0] H
print("var[2]", var2[1:5])  #Intercept the second to fifth characters in the second string and output: var[2] ytho

        In addition, the str[beg:], str[:end], str[beg:end] and str[:-index] methods of the string can also be used to realize the interception operation, for example:

str = '0123456789'
print(str[0:3])         #Intercept the character output from the first bit to the third bit: 012
print(str[:])           #Intercept all characters in the string and output: 0123456789
print(str[6:])          #Intercept all characters after the sixth character output: 6789
print(str[:-3])         #Intercept the character output from the beginning to the penultimate character: 0123456
print(str[2])           #Intercept the third character output: 2
print(str[-1])          #Intercept the penultimate character output: 9
print(str[::-1])        #Create a string output in the reverse order of the original string: 9876543210
print(str[-3:-1])       #Intercept the character output before the penultimate third bit and the penultimate first bit: 78
print(str[-3:])         #Intercept the penultimate bit to the end output: 789

1.2 characters

  1.2.1 escape character  

Escape characters commonly used in Python
Escape characterdescribe
\(at the end of the line)Continuation character
\\Backslash symbol
\'Single quotation mark
\''Double quotation mark
\aRing the bell
\bBackspace
\eEscape
\000empty
\nNewline character
\vVertical tab
\thorizontal tab
\rCarriage return
\fPage feed
\oyyOctal, the character represented by yy, such as "\ o12"   Represents a newline character
\xyyHexadecimal, the character represented by yy, for example   "\ x0a" stands for line break
\otherOther characters are output in normal format

1.2.2 formatting characters

Common string formatting symbols in Python
Symboldescribe
%cFormatted characters and their ASCII codes
%sformat string
%dFormat integer
%uFormat unsigned integer
%oFormat unsigned octal number
%xFormat unsigned hexadecimal number
%XFormat unsigned hex (uppercase)
%fFormat floating-point numbers to specify the precision after the decimal point
%eFormatting floating point numbers with scientific counting
%EThe function is the same as%e, format floating-point numbers with scientific counting method
%g%Abbreviations for f and% e
%G%Abbreviations for f and% E
%pFormat the address of a variable with a hexadecimal number

1.3 string processing function

String processing functions commonly used in Python
String handlerdescribe
String.capitaliza()Capitalizes the first letter of the string
String.count()Gets the number of a substring in a string
String.casefold()Returns the version suitable for no case comparison (the same usage as String.lower)
String.center()Center alignment
String.ljust()Align left
String.rjust()Right align
String.endswith()Detect whether it ends with a string (bool type)
String.find()Gets the starting position of a substring in a string, and returns - 1 if none
String.isalnum()Check whether the string contains only numbers or English letters
String.isalpha()Check whether the string contains only a ~ Z and a ~ Z
String.isdigit()Check whether the string contains only 0 ~ 9
String.islower()Detects whether all strings are lowercase letters
String.isspace()Detects whether all characters in the string are white space characters
String.istitle()Detects whether the first letter of a word in a string is capitalized
String.isupper()Detects whether the strings are all uppercase letters
String.jion()Connection string
String.lower()Convert all strings to lowercase
Sting.split()Split string
String.swapcase()Converts uppercase letters to lowercase and lowercase letters to uppercase in a string
String.title()Capitalizes the first letter of a word in a string
String.upper()Converts all letters in a string to uppercase
len(String)Gets the length of the string

1.fromat method

#format method
# "". Format (parameter)
# '{}'. Format (parameter)
print(f"data is {data:*^4}, data2 is {data2}, data3 is {data3}")
# {} correspondence, according to the order of parameters passed later
print("data is {}, data2 is {}, data3 is {}".format(data, data2, data3))
# You can also specify the order: {0}, {1}, {2}
# The premise is to treat the parameters passed in by format as a tuple (data, data2, data3)
print("data is {2}, data2 is {1}, data3 is {0}".format(data, data2, data3))
# Define a list
list_data = [1, 2, 3]
# [1, 2, 3] is equivalent to an element of a tuple, and subscripts are used to get the elements of the list in the tuple
print("data is {0[0]}, data2 is {0[1]}, data3 is {0[2]}".format([1, 2, 3]))     
#Write out the list [1, 2, 3] in a dictionary - > list_data = [1, 2, 3], list_data is equivalent to the key in the dictionary, and take out the elements by using the knowledge in the dictionary
print("data is {list_data[0]}, data2 is {list_data[1]}, data3 is {list_data[2]}".format(list_data=[1, 2, 3]))   

2.expandtabas method

           Returns a copy of a string in which all tabs are replaced by one or more spaces, depending on the current column position and the given tab width. Each tabsize character is set to one tab stop (the tab stops set when the default value is 8 are in columns 0, 8, 16, and so on) . to expand the string, the current column is set to zero and each character in the string is checked one by one. If the character is a tab (\ t), one or more space characters are inserted into the result until the current column equals the next tab stop. (the tab itself is not copied.) if the character is a newline (\ n) or carriage return (\ r) , it will be copied and reset the current column to zero. Any other characters will be copied unmodified and the current column will be incremented by one, regardless of how the character will appear when printed.

str_data = "hello\tworld\thello"
print(str_data)
print(str_data.expandtabs(tabsize=2), len(str_data.expandtabs(tabsize=2)))    #Output: Hello 	 world 	 hello
print(str_data.expandtabs(tabsize=3), len(str_data.expandtabs(tabsize=3)))    Output: hello world hello 17
print(str_data.expandtabs(tabsize=4), len(str_data.expandtabs(tabsize=4)))    Output: hello   world   hello 21
print(str_data.expandtabs(tabsize=5), len(str_data.expandtabs(tabsize=5)))    Output: hello     world     hello 25
print(str_data.expandtabs(tabsize=9), len(str_data.expandtabs(tabsize=9)))    Output: hello    world    hello 23

Topics: Python string