Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果某些字符不在字符串中的特定位置,请删除这些字符_Python_Python 3.x - Fatal编程技术网

Python 如果某些字符不在字符串中的特定位置,请删除这些字符

Python 如果某些字符不在字符串中的特定位置,请删除这些字符,python,python-3.x,Python,Python 3.x,我试图从python类中找出以下函数情况。我已经得到了删除这三个字母的密码,但是他们不希望我这么做。即将WGU从第一条线移除,而不是从WGUJohn移除 # Complete the function to remove the word WGU from the given string # ONLY if it's not the first word and return the new string def removeWGU(mystring): #if mystring[0]

我试图从python类中找出以下函数情况。我已经得到了删除这三个字母的密码,但是他们不希望我这么做。即将WGU从第一条线移除,而不是从WGUJohn移除

# Complete the function to remove the word WGU from the given string
# ONLY if it's not the first word and return the new string
def removeWGU(mystring):
    #if mystring[0]!= ('WGU'):
        #return mystring.strip('WGU')
    #if mystring([0]!= 'WGU')
        #return mystring.split('WGU')
    
# Student code goes here
    
# expected output: WGU Rocks
print(removeWGU('WGU Rocks'))
    
# expected output: Hello, John
print(removeWGU('Hello, WGUJohn'))
勾选这个:

def removeWGU(mystring):
    s = mystring.split()
    if s[0] == "WGU":
        return mystring
    else:
        return mystring.replace("WGU","")
print(removeWGU('WGU Rocks'))
print(removeWGU('Hello, WGUJohn'))
def removeWGU(mystring):
返回mystring[0]+mystring[1:://替换(“WGU”,“WGU”)
我看到的其他回答对于文本中有多个“WGU”且开头有一个的尖锐案例不起作用,例如

打印(删除WGU(“WGU,其他东西,另一个WGU…”)

让堆栈溢出来回答你的家庭作业问题不是学习python的好方法。你需要拆分mystringfirst@7koFnMiP做什么?当前,该片只是禁止替换函数在3个第一个字符上触发。
mystring[0]
指的是
W
,在本例中不是
WGU
,这是您想要的吗?是的,通过从replace()函数中删除第一个字符,替换可以触发文本中的任何“WGU”,除非它位于完整字符串的最开头,因为“W”将丢失。例如,“aWGUbcdef”也将更改为“abcdef”