Python strip() 在函数内不起作用 [英] Python strip() not working inside a function
本文介绍了Python strip() 在函数内不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 strip()
来修剪字符串前后的空格.它适用于
I am trying to use strip()
to trim off the space before and after a string. It works fine for
str1 = " abdced "
str1.strip()
但是,当我在函数中使用它时,它不起作用:
However when I use it inside a function, it is not working:
def func(str1):
return str1.strip()
print func(" abdwe ")
它不会剪掉任何空间.任何人都可以告诉发生了什么?谢谢!
It won't trim off any space. Anyone can tell what's happening? Thanks!
推荐答案
strip
不是就地方法,这意味着它返回一个必须像这样重新分配的值:
strip
is not an in-place method, meaning it returns a value which must be reassigned like so:
str1 = str1.strip() # the string is reassigned to the returned stripped string
这篇关于Python strip() 在函数内不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文