程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python (VII) -- detailed use of strings

編輯:Python

List of articles

  • Python( 7、 ... and )—— Detailed use of strings and lists

Python( 7、 ... and )—— Detailed use of strings and lists

String splicing

Mode one : Put two string constants next to each other , It will be spliced when printing

str3 = 'Process finished ''with exit code'
print(str3)

Execution results : This method is only applicable to the splicing of string constants

Process finished with exit code

Mode two : Use + Splice two strings

str1 = 'Process finished '
str2 = 'with exit code'
print(str1+str2)
str3 = 'Process finished '+'with exit code'
print(str3)

Execution results : In this way, string constants and variables can be spliced

Process finished with exit code
Process finished with exit code

Mode three :join() Method ,join() The list will be ( Or tuples ) Multiple strings in are spliced together using the specified delimiter

my_list = ['www','weipc','top']
print('.'.join(my_list))
str14 = 'x'
print(str14.join("python"))

Execution results :

www.weipc.top
pxyxtxhxoxn

String and number splicing

Splice strings and numbers , You need to first convert the number from numeric type to string type , have access to str() Method

name = " Li Xiaoyao "
age = 18
info = name + " already " + str(age) + " Year old "
print(info)

Execution results :

 Li Xiaoyao has 18 Year old

Get string length len()

str4 = 'Process finished The process is complete '
# Get string length 
print(len(str4))
# Get string adoption UTF-8 Number of bytes when encoding 
print((len(str4.encode())))
# Get string adoption GBK Number of bytes when encoding 
print((len(str4.encode('gbk'))))

Execution results :

21
29
25

Find content find()

Used to retrieve whether the specified content exists in the string , If it exists, it returns the index value of the first occurrence of the content in the string , Otherwise, return -1

str5 = 'Process'
print(str5.find('ess'))
print(str5.find('a'))

Execution results :

4
-1

startswith() and endswith()

startswith() Method is used to determine whether the string starts with the specified string , Is to return True; Instead, return to False.

endswith() Method is used to determine whether the string ends with the specified string , Is to return True; Instead, return to False.

str6 = 'Process'
print(str6.startswith('P'))
# Judge from the specified position of the string 
print(str6.startswith('c',3))
print(str6.endswith('s'))
print(str6.endswith('t'))

Execution results :

True
True
True
False

Count the number of occurrences count()

count Method is used to return the number of times a specified string appears in another string , There is no return 0, Existence returns the number of occurrences .

str7 = 'Process finished in the'
str8 = 'in'
str9 = 'abc'
print(str7.count(str8))
print(str7.count(str9))
# Specify the starting location of the search 
print(str7.count(str8,10))
# Specify the starting and ending positions of retrieval 
print(str7.count(str8,12,20))

Execution results :

2
0
1
1

Replace string replace()

replace() Method replaces the old string with the new string .

str10 = 'Process finished'
print(str10.replace('s','x'))
# Specify the maximum number of replacements 
print(str10.replace('s','x',2))

Execution results :

Procexx finixhed
Procexx finished

Split string split()

split() Method to cut a string into multiple substrings according to the specified delimiter , These substrings will be saved to the list ( Does not contain separators ), Feedback back as the return value of the method .

str11 = 'Process finished with exit code'
print(str11.split(' '))
print(str11.split('i'))
print(str11.split('i',2))

Execution results :

['Process', 'finished', 'with', 'exit', 'code']
['Process f', 'n', 'shed w', 'th ex', 't code']
['Process f', 'n', 'shed with exit code']

toggle case title()、lower() and upper()

title(): Only convert the initial letter to uppercase , Convert others to lowercase

lower(): Convert strings to uppercase

upper(): Convert string to lowercase

str12 = 'Process finished With EXIT code'
print(str12.title())
print(str12.upper())
print(str12.lower())

Execution results :

Process Finished With Exit Code
PROCESS FINISHED WITH EXIT CODE
process finished with exit code

Remove spaces and special characters from strings strip(),lstrip(),rstrip()

strip(), Remove spaces or special characters on the left and right sides of the string

lstrip(), Remove spaces or special characters to the left of the string

rstrip(), Remove spaces or special characters on the right side of the string

Special characters refer to tabs (\t)、 A carriage return (\r)、 A newline (\n) etc. .

str13 = ' Process finished with exit code '
print(str13.strip())
print(str13.lstrip())
print(str13.rstrip())
# Remove the character sequence specified at the beginning and end of the string 
print(str13.strip('e '))

Execution results : Returns a copy of the string after the space is deleted , It does not change the original string

Process finished with exit code
Process finished with exit code
Process finished with exit code
Process finished with exit cod

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved