简体   繁体   English

python程序中的意外输出

[英]Unexpected output in python program

I have copied the following code with some alteration in naming to solve the problem as stated: Given a string s, return a string where all occurences of its first char have been changed to '*', except do not change the first char itself. 我复制了以下代码,并做了一些改动,以解决上述问题:给定字符串s,返回一个字符串,其中所有出现的第一个字符都已更改为'*',但不更改第一个字符本身。 eg 'babble' yields 'ba**le'. 例如,“ ba”会产生“ ba ** le”。 Assume that the string is length 1 or more. 假设字符串长度为1或更大。 Here is the code: My question is when I pass "Lulla" why don't I see "Lu**a" returned in the interpreter as I think the code should do. 这是代码:我的问题是,当我通过“ Lulla”时,为什么我看不到解释器返回的“ Lu ** a”,因为我认为代码应该这样做。

def fix_start(s):
    start_letter = s[0]
    rest_letters = s[1:]
    rest_letters = rest_letters.replace(start_letter,'*')  
    return start_letter + rest_letters

print (fix_start ('Lulla'))

What happens is that Python is case-sensitive . 发生的事情是Python 区分大小写 In other words: 换一种说法:

'L' != 'l'

So, when you do 所以,当你这样做

rest_letters = rest_letters.replace(start_letter,'*')

it will replace all occurences of only L , not both L and l . 它将仅替换L所有出现,而不替换Ll

What can you do? 你能做什么? The process of case-insensitive replacing is kind of complex, as you can see in these answers . 如您在这些答案中所见,区分大小写的替换过程有点复杂。 But in your case, this may work: 但是在您的情况下,这可能起作用:

rest_letters = rest_letters.replace(start_letter,'*').replace(start_letter.lower(), '*')

In your code, you are replacing any instance of the first letter "L" in the remainder of the string. 在您的代码中,您要替换字符串其余部分中第一个字母“ L”的任何实例。 In the case of the example, "Lulla", the first letter is a capital "L", and is not equal to "l". 在示例“ Lulla”的情况下,第一个字母为大写字母“ L”,并且不等于“ l”。 As such, I would try the following: 因此,我将尝试以下方法:

def fix_start(s):
    start_letter = s[0]
    rest_letters = s[1:]
    rest_letters = rest_letters.replace(start_letter.lower(),'*')
    return start_letter + rest_letters

The above solution will work if you have strings that are guaranteed to be syntactically correct (as in no uppercase letters in the middle of the word). 如果您的字符串在语法上正确无误(例如单词中间没有大写字母),则上述解决方案将起作用。

If that is not guaranteed, go ahead and try this: 如果不能保证,请尝试以下操作:

def fix_start(s):
    start_letter = s[0]
    rest_letters = s[1:]
    rest_letters = rest_letters.replace(start_letter.lower(),'*')
    rest_letters = rest_letters.replace(start_letter, '*')
    return start_letter + rest_letters

Actually your trying to replace L but rest of the characters are small l .Try using regular expression that makes this task easy. 实际上,您尝试替换L但其余字符很小l请尝试使用正则表达式使此任务容易进行。

import re
def fix_start(s):
    start_letter = s[0]
    regex = s[0].lower()+"|"+s[0].upper()
    rest_letters = s[1:]
    rest_letters = re.sub(regex, "*", rest_letters) 
    return start_letter + rest_letters

print (fix_start ('Lulla'))

Quite simply all that is required is to use lower() or upper() to change the string case: 非常简单,仅需要使用lower()upper()来更改字符串的大小写:

start_letter = s[0].lower()
rest_letters = s[1:].lower()

Input: LulLa 输入: LulLa
Output: lu**a 输出: lu ** a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM