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

String method based on python (Part 2)

編輯:Python

About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .


One 、 background

Python Is an easy to learn 、 Powerful programming language . It provides an efficient high-level data structure , It's also a simple and effective way of object-oriented programming .Python Elegant grammar and dynamic typing and the essence of interpretive language , Make it an ideal language for scripting and rapid application development on most platforms . Now let's introduce python String related methods .


Two 、 Related methods

1、replace() Method

replace() Method is used to replace an old string with a new one in a string .. The format is as follows . (1)str Represents the searched string ; (2)old Represents the substring to be replaced ; (3)new Represents a new string , Used for replacement old Substring ; (4)max Is an optional parameter , Indicates that the replacement does not exceed max Time , Replace all by default when omitted .

str.replace(old,new[,max])

example : Create string “new_str = ”Monday Tuesday Wednesday Thursday Friday Saturday Sunday“”, Use replace() Method to replace day by DAY.new_str in .

new_str = "Monday Tuesday Wednesday Thursday Friday Saturday Sunday"
a=new_str.replace('day','DAY') # take new_str Medium day Replace with DAY
b=new_str.replace('day','DAY',3) # take new_str Medium day Replace with DAY, Limit to 3 Time
print("a:",a)
print("b:",b)

The operation results are as follows :

Another common method for finding substrings is index() Method , The method and find() The usage of methods is basically the same , The difference is that when the substring you are looking for does not exist , Throw an exception .


2、strip() Method

strip() Method is used to delete consecutive white space characters or specified characters at both ends of a string , The format is as follows . (1)str Representation string ; (2)chars Means to remove the specified characters at both ends of the string , When omitted, the default is space .

str.strip([chars])

example : Create string new_str = “110This is an example 0001”, Use strip() Method to remove... From the string “0” and “1”.

new_str = "110This is an example 0001" # Create string
a=new_str.strip('1') # remove new_str On both sides 1
b=new_str.strip('01') # remove new_str On both sides 0 and 1
print("a:",a)
print("b:",b)

The operation results are as follows :


3、lower() Method

lower() Method is used to convert all uppercase characters in a string to lowercase .

str.lower()

example : Create string “new_str=”This is an EXAMPLE“”, Use lower() Method to convert it to lowercase .

new_str = "This is an EXAMPLE" # Create string
a=new_str.lower() # take new_str Convert uppercase characters in to lowercase
print("a:",a)

The operation results are as follows :


4、upper() Method

upper() Method is used to convert all lowercase characters in the string to uppercase .

str.lower()

example : Create string “new_str=”This is an EXAMPLE“”, Use upper() Method to convert it to uppercase .

new_str = "This is an EXAMPLE" # Create string
a=new_str.upper() # take new_str Convert lowercase characters in to uppercase
print("a:",a)

The operation results are as follows :


5、isalnum() Method

isalnum() Method is used to detect whether a string is composed of letters and numbers , Or a combination of two . If so, go back True; Otherwise return to False.

str.isalnum()

example 1: Create string new_str=“2018example”, Use isalnum() Methods to judge new_str Whether the consists of letters or numbers .

new_str = "2018example" # Create string
a=new_str.isalnum() # Judge new_str Whether there are only numbers or letters in
print("a:",a)

The operation results are as follows :

example 2: The string “This is a python book!“ Delete the extra space in , That is, if there are consecutive spaces, only one .

new_str = "This is a python book!" # Create string
s_str=new_str.split() # Use empty characters as separators , take new_str Divide it all
print(s_str) # Output the result after segmentation
j_str=' '.join(s_str) # Connect... With spaces s_str The characters in
print(j_str) # Output the connected string

The operation results are as follows :


3、 ... and 、 Reference resources

1、 Liao Xuefeng's official website 2、python Official website 3、Python Programming case tutorial


Four 、 summary

The above is about Python Knowledge of string methods , There are mainly replace(),strip(),lower(),upper(),isalnum() Method . You can refer to it , Relevant knowledge will be continuously updated later , Make progress together .


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