python 里面怎么提取 空格分开的字符串

如题所述

1、借助于lstrip()提取左边空格

>>> s = ' A B  C '

>>> s.lstrip()        # 去除字母字符串左边的空格

'A B  C '

2、借助于rstrip()提取右边空格

>>> s = " A B  C "

>>> s.rstrip()        # 去除字符串右边的空格

' A B  C'

3、借助于strip()提取左右两边的空格

>>> s = " A B  C "

>>> s.strip()         # 去除两边的空格

'A B  C'

扩展资料

python对象的处理方法

对象的方法是指绑定到对象的函数。调用对象方法的语法是instance.method(arguments)。它等价于调用Class.method(instance, arguments)。

当定义对象方法时,必须显式地定义第一个参数,一般该参数名都使用self,用于访问对象的内部数据。

这里的self相当于C++, Java里面的this变量,但是我们还可以使用任何其它合法的参数名,比如this 和 mine 等,self与C++,Java里面的this不完全一样,它可以被看作是一个习惯性的用法,我们传入任何其它的合法名称都行。

参考资料

百度百科-python



温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-12-28
mystr = "this is a string, with words!"

import re
word = re.sub("[^\w]", " ", mystr).split()


Further Explanation

re.sub(pattern, repl, string, count=0, flags=0)

/*
    Return the string obtained by replacing
    the leftmost non-overlapping occurrences of pattern
    in string by the replacement repl.
    If the pattern isn’t found, string is returned unchanged.
    repl can be a string or a function.
    
    [\w] means any alphanumeric character
 */

本回答被提问者采纳
第2个回答  推荐于2017-06-27
s='hello! my word'
print s.split()
#输出结果为['hello!', 'my', 'word']

本回答被网友采纳