小编Hom*_*mer的帖子

如何在python中替换和插入一个新的子字符串?

这是一个工作代码,可能不是用先前修改的另一个子字符串替换子字符串的有效代码

输入字符串:

text = ["part1 Pirates (2006)",
        "part2 Pirates (2006)"
]
Run Code Online (Sandbox Code Playgroud)

输出字符串:

 Pirates PT1 (2006)

 Pirates PT2 (2006)
Run Code Online (Sandbox Code Playgroud)

它必须用 'PT' 替换子字符串,如 'part1' 'part2 等等,并将其复制到标题和年份子字符串之间 代码:

#'''''''''''''''''''''''''
# are there parenthesis?
# 
def parenth(stringa):
   count = 0
  for i in stringa:
     if i == "(":
        count += 1
     elif i == ")":
        count -= 1
     if count < 0:
        return False
  return count == 0 


#'''''''''''''''''''''''''
# extract 'year' from 
# the string
# 
def getYear(stringa):
     if parenth(stringa) is True:
      return …
Run Code Online (Sandbox Code Playgroud)

python regex substring title

6
推荐指数
1
解决办法
151
查看次数

标签 统计

python ×1

regex ×1

substring ×1

title ×1