Data type String Python

String

Python strings are more commonly used data types. We can use quotation marks ( "or") create a string.

string = "hello world"

It is an array of strings

String represents in Python byte array of Unicode characters. Use square brackets []can access the elements of the string.

String is an ordered set of characters, concrete elements can be obtained by its position. In python, the character string is extracted by an index, the index starts from 0.

python may be negative, showing extracts from the end of the last -1, -2 penultimate, i.e. the program can count backwards from that end.

Sample code:

string = "hello world"
print(string[0])
print(string[-5:-2])
print(string[::-1])

Note: [start:stop:step]similar range () function, care regardless tail.

Get the length of the string

len () function to get the length of the string, len () function we will often use the data type.

Sample code:

string = "hello python"
print(string)
print("字符串的长度是", len(string))

Common method string

Since there are many ways to string, the film article only describes commonly used method.

strip () to delete the beginning or end of a blank character

string = '   helllo   '
print(string.strip())

count () Gets the number of times a character appears in a string

string = "hello python"
print("字符o的次数是:",string.count('o'))

find () returns the index to find the character, a plurality of the first return, the absence of characters -1

string = "hello python"
print(string.find('n'))
print(string.find('a')) # 打印-1

index () returns the index to find the character, a plurality of first return, there is no direct error

string = "hello python"
print(string.index('l'))
print(string.index('a')) # 直接报错

replace (oldstr, newstr) string replacement

string = "hello python"
new_str = string.replace('p',"P")
print(new_str)

format () format string

string = 'hello{}'
new_str = string.format('你好')
print(new_str)

startswith () endswith () to determine the beginning and end characters

string = "hello Python"
print(string.startswith('h'))
print(string.endswith('n'))

split () divided string

string = "hello world"
print(string.split(" "))

Note: Split string returns a list, which is separated list of strings.

join () connection string

string = "hello"
new_str = '你好'.join(string)
print(new_str)

Adding the string, the string is multiplied

str1 = "hello"
str2 = "world"
new_str1 = str1 + str2
new_str2 = str1 * 3
print(new_str1)
print(new_str2)

It can be found in: adding only one character string string concatenation, and the string is copied and multiplied by the same number of three strings.

String traversal

Recommended for loops, while loops are generally used as an endless loop.
Code Example:

string = "hello world"
for i in string:
    print(i)

Above only describes some of the methods of the string, if you feel a herein may be looking at trouble spots. Your support is my greatest motivation.

Guess you like

Origin www.cnblogs.com/liudemeng/p/12171043.html