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

Insert other characters before each character in the Python string

編輯:Python
background : Posted a comment on a website , I don't know what I should not touch , The station automatically shielded me , Just the latest degree python, Think about adding... Before each character ’' Should be able to escape the clutches of automatic audit
requirement : Precede each character of a particular string with a slash (\)

str1=‘123456abcdefg’
def fun(str):
for i in range(0, len(str), 1):
print(str[i:i + 1], ‘/’, end=‘’)
fun(str1)

Output :
1 /2 /3 /4 /5 /6 /a /b /c /d /e /f /g /
Process finished with exit code 0

summary : The core of the short code is to disassemble the string and output
Then I read something else to say replace() The built-in functions are troublesome and can only replace specific characters
Another one spilt() function , yes
str.split(sep,maxsplit)
The meanings of the parameters in this method are respectively :
str: Represents the string to be split ;
sep: Used to specify separator , Can contain multiple characters . This parameter defaults to None, Indicates all empty characters , Including Spaces 、 A newline “\n”、 tabs “\t” etc. .
maxsplit: Optional parameters , Used to specify the number of divisions , The number of substrings in the final list is at most maxsplit+1. If not specified or designated as -1, It means that there is no limit to the number of segmentation .
This method is to move the string along the original space ( Or a specific character ) Open , After using regular expressions import re String can be split along multiple specified characters
For details, please refer to the link :
https://blog.csdn.net/sinat_38682860/article/details/80375369


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