简体   繁体   English

如何定义以小写形式返回我的字符串的 function?

[英]How do I define a function that returns my string in lower case?

I am trying to define a function that makes a string of text inputed into lowercase.我正在尝试定义一个 function ,它将一串文本输入小写。

def lowercase(text:str):
     text.lower()
     return text

Once I put my test sentence into this function, it still prints out uppercase letters.一旦我将我的测试语句放入这个 function,它仍然会打印出大写字母。

.lower() returns a new value, therefor you need to store it in a variable or directly return this. .lower()返回一个新值,因此您需要将其存储在变量中或直接返回它。

def lowercase(text:str):
     text = text.lower()
     return text

or或者

def lowercase(text:str):
     return text.lower()
def lowercase(text:str):
     return text.lower()

This should do it.这应该这样做。

Try this:尝试这个:

def lower(text):
    lowered = text.lower()
    return lowered

You were returning text which was the input of the function, so the function returned the text which it received as input.您返回的text是 function 的输入,因此 function 返回了它作为输入接收的文本。

暂无
暂无

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

相关问题 如何使用循环创建 function 以返回 python 上大写和小写字母的数量? - How do I create a function using loops to return the number of upper case and lower case letters on python? 如何在Python中将输入默认字典切换为小写以进行NLTK比较 - How do I switch my input default dictionary to lower case for NLTK comparison in Python 如何定义 function 将字符串拆分为单独的行? - How do I define a function to split a string into separate lines? 如何在Python中组织大小写单词? - How do I organize lower and upper case words in Python? 如何在 Python 中定义自己的范围 function - How do I define my own range function in Python Swift - 如何为我的类定义一个特殊方法,该方法返回其对象的字符串表示 - Swift - How to I define a special method for my class that returns a string representation of its object 如果数据类型是字符串等,如何定义提供数据并返回 True 的 function? - How can I Define a function that offers a data and returns True if the data type is string, etc.? 我怎么知道函数返回的是字符串还是字节? - How do I know if a function returns string or bytes? 计算字符串中小写字母和大写字母个数的 Python 函数 - Python function that counts number of lower case and upper case letters in a string 如何将缩写序数的大小写更低,同时将标题的其余部分保留在标题大小写中? - How can I change the case of abbreviated ordinals to lower while keeping the rest of the string in title case?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM