instead of printing 12359 it prints 9 for some reason
code
b=["12","3","5","9"]
b1=[]
for x in range(0,len(b)):
b1=b[x]
print(b1,end='')
output
9
instead of printing 12359 it prints 9 for some reason
b=["12","3","5","9"]
b1=[]
for x in range(0,len(b)):
b1=b[x]
print(b1,end='')
9
Well, if you want to concatenate the elements of b into a string, just try:
b = ["12", "3", "5", "9"]
b1 = ''.join(b)
Explanation:
Help on built-in function join:
join(iterable, /) method of builtins.str instance
Concatenate any number of strings.
The string whose method is called is inserted in between each given string.
The result is returned as a new string.
Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'
If you want to concatenate all strings, you need to use a string for b1:
b = ["12", "3", "5", "9"]
b1 = ""
for x in range(0, len(b)):
b1 += b[x]
print(b1)
b1 is not a list. Its is a string. If you to have a list, do this:
b=["12","3","5","9"]
b1=[]
for x in range(0,len(b)):
b1.append(b[x])
print(b1) # Prints the exact same as b.
If you want an output string, you should add it to an empty string like so:
b=["12","3","5","9"]
b1=""
for x in range(0,len(b)):
b1 += b[x]