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

Python Basics_ Splicing, escape, segmentation, replacement and formatting of common string methods

編輯:Python
# split() Division Separator :
# sep: Separator : Will not appear in the split data
person_info = ' I am a XX teacher , I like "python30 period ", I have a holiday with you today !'
pain = person_info.split(',') # split( data ) Segmentation
print(pain) # Print Will split the string into a list ( There are three sections in it )
# maxsplit 1 Number of divisions
pain1 = person_info.split(',',1) # maxsplit( data , frequency ) Segmentation
print(pain1) # Print Will split the string into a list ( There is a passage in it )
# join() Splicing Splice them together according to the separator
# Ba fragmented several strings , Spliced into a complete string
# 1、 Splicer : character string ;
ss = ";".join(pain) # Use ; Semicolon will split the list with join() Splice up
print(ss) # It's printed out ; The complete string concatenated by
# Replace character operation
# Format replace( The original string must be replaced , New characters )
# Cannot change the current string , Only a brand new string will be produced
a = person_info.replace(' I ',' you ') # take ' I ' Replace with ' you '
print(a) # Print and reproduce a new string
# Format output format() function
# usage : character string .format()
# Dynamically change the value of the string : Place holder {}, Use braces , There are several placeholders {}, You need a few replacement parameters
# There is no need to specify the data type , All data types support
# The first method , No serial number , Sequential assignment
age = input(' Age :') # use input() Output function
name = input(' name :')
print(' My age is :{} year , My name is :{}'.format(age,name)) # Use format() function
# The second method , According to the position of the serial number , To assign
# There is some data that needs to be reused , In placeholder {} Add index to
# {0} {1} {2} According to the position of the serial number , To assign
print(' My age is :{0} year , My name is :{1}, My hobby is :{2}, What do I like to do :{2}'.format(age,name,' sleep ')) # Add index
# The third kind of Retain 2 Decimal places
# Format :{:.2f} f Represents a floating point number ,2 Express reservation 2 position ( It can also be set without )
print(' My grade is {:.2f} branch '.format(89.79788)) # To round
# A fourth Percent sign % 100% format
# Format :{:.2%} % It's a percent sign , The data is automatically multiplied by 100
print(' The degree of completion is {:.2%}'.format(0.28))
# String splicing
# Splicing + Must be a string
str1 = ' Hello everyone ,'
str2 = ' I'm new here ,'
str3 = ' To take his place !'
print(str1 + '\t' + str2 + '\t'+ str3) # use + The number connects strings together , Sure \t spacing
# Escape character
# The first one is :\n Line break
print('hello,world!!\n Hello, I'm interviewing \nhello,python!!') # \n Means line break
# The first one is :\t empty tab
print('hello,world!!\t Hello, I'm interviewing \thello,python!!') # \t empty tab
# Close escape ( Path representations are useful )
print(r'hello,world!! \n Hello, I'm interviewing \n hello,python!!') # r Means to cancel escape , Normal output

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