python编程数据字符串部分

用户输入一个任意长度的数据字符串,其中的数据以逗号分隔(例如:'a,  b,c   ,   d  ,e' ),并且数据的前后可能有多余的空格。将数据存于一个列表,并且把列表中数据前后的空格去掉。(例如:['a', 'b', 'c', 'd', 'e'])注意,不能使用str的strip方法。

 

source='a, b,c , d ,e'

t=[]
for i in source:
    if not(( i == " " )or  (i ==',')):
        t.append(i)

print(t)

 

 

s = 'a,  b,c   ,   d  ,e'
r = []
for i in s.split(','):
    for j in i:
        if not ' ' in j:
            r.append(j)
print(r)

如果对你有帮助,可以点击我这个回答右上方的【采纳】按钮,给我个采纳吗,谢谢
 

源程序如下:

strings = input("从键盘输入一串字符:")
strList = list(strings)
newList = []
for i in strList:
    if not (i==" " or i==","):
        newList.append(i)
print("转化后的字符列表:")
print(newList)

输出结果如下:

从键盘输入一串字符:a  ,b ,  c,d, e ,f   ,g
转化后的字符列表:
['a', 'b', 'c', 'd', 'e', 'f', 'g']

 

 

给你一个最简洁的方法,两行搞定。 

a = input('输入:')
b = [a.replace(' ','')]
print(b)


满意请采纳哦!谢谢。

s = input("请输入:")
l = [];
for index, item in enumerate(s):
    if item == ",":
        l.append(index)
if l[len(l) - 1] != len(s):
    l.append(len(s))
print(l)
sl = []
for index, item in enumerate(l):
    if index == 0:
        sl.append(s[0: item])
    else:
        sl.append((s[l[index - 1] + 1: item]).strip())
print(sl)
# 1, qweq sdf1, dxvd ,sdf we, sdfsdxcvwe df