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

Python string

編輯:Python

String or string (String) It's numbers 、 Letter 、 A string of characters made up of underscores . stay Python in , The string is enclosed in single quotation marks or double quotation marks .

Catalog

String splicing

String processing (8 Common use )


String splicing

Put together a comma , Then a space will appear in the comma .

Use the plus sign to splice , There are no spaces . If you want spaces , Then a space should be added

print("Hello","World") # Output :Hello World
print("Hello"+"World") # Output :HelloWorld
print("Hello"+" "+"World") # Output :Hello World

String processing (8 Common use )

len(): Returns the length of the string , That is, the number of single elements in the string

length=len(target_string) #target-string Is the target string variable 

upper(): Convert all characters in the string to uppercase

lower(): Convert all characters to lowercase

title(): Change the first letter of all words in the string to uppercase , The other letters are still lowercase

new_string="Tom is good"
print(name_string.upper()) # Output :TOM IS GOOD
print(name_string.lower()) # Output :tom is good
print(name_string.title()) # Output :Tom Is Good

strip(): You can remove both sides of the string ( Does not include internal ) All spaces . Using this method , You can also specify parameters , Remove specific characters specified on both sides

split(): Implement string splitting . This method is based on the provided delimiter , Split a string into a list of characters , If no delimiter is provided , The program will default to the space ( Tabulation 、 Line break, etc ) As a separator .

find(): Using this method, you can find substrings in a long string . If in this string , There are one or more substrings , The method returns the leftmost index of the first substring , If no qualified substring is found , Then return to -1.

replace(): To replace substrings in a given string .

source_string.replace(old_string, new_string) 


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