Home > Mobile >  cant resplit after cicle. it says "AttributeError: 'list' object has no attribute �
cant resplit after cicle. it says "AttributeError: 'list' object has no attribute �

Time:11-15

cant split textNoZap after cicle (it says "AttributeError: 'list' object has no attribute 'join'"). Can somebody say what wrong i do:

text=str(input("tap you text here: "))
textNoZap=[0]
for leter in text:
    if leter != ",":
        textNoZap.append(leter)
    else:
        textNoZap.append(" ")
del textNoZap[0]
print(textNoZap)
textNoZap=textNoZap.join()
textNoZap=textNoZap.split()
print(textNoZap)

CodePudding user response:

You wrote textNoZap.join(), but that is the wrong way of using that function. You should write it like that:

textNoZap = ''.join(textNoZap)

In those quotes before .join() you put anything you need to join your list with

CodePudding user response:

Both 'join' and 'split' are methods of the string object, not list object. That is why you get this error. Your question can be easily solved by a quick google search yourself.

CodePudding user response:

I don't know your expected behaviour, I'm assuming you want to try to change all the commas in a string to spaces.

Lists have no function split() or join()

The simple and easy method (builtin):

text=str(input("tap you text here: "))
text = text.replace(",", " ")    #Replaces ',' with ' '
print(text)

The method with lists that it seems you want:

text = str(input("tap you text here: "))
textNoZap = []
for leter in text:
    if leter != ",":
        textNoZap.append(leter)
    else:
        textNoZap.append(" ")

final_text = ''
for i in textNoZap:
    final_text  = i
print(final_text)

Or (From FSTMAX answer):

text = str(input("tap you text here: "))
textNoZap = []
for leter in text:
    if leter != ",":
        textNoZap.append(leter)
    else:
        textNoZap.append(" ")
textNoZap = ''.join(textNoZap)
print(textNoZap)
  • Related