Is there a convenient way to delete the text you want from Python?

Asked 2 years ago, Updated 2 years ago, 16 views

a = '123456789'
a[:-1]

If you slice it like the above way, it's not a deletion price, but it's hidden? I lost and said, "12345678" If you actually call the value, it's 123456789 even if it's shown. Whether only the last end can be deleted (backspace) I'd like to know

Also, I would like to ask you to combine the list of branches.

b = []
for i in range(5):
    a = input(':')
    b.append(a)
    print(b)

[1]
[1].[2]
[1].[2].[3]
...

I want to print out ['1,2,3,4,5~'] at once, but extend is not working and the + symbol is not working. Is there any other way in the repeat sentence? I'd appreciate it if you let me know.

python

2022-09-22 08:55

1 Answers

a[:-1]

The Iranian syntax returns a new string after the slice. Therefore, to delete the ending character

a = a[:-1]

You must assign the newly returned string to the original variable as shown above.

I don't know exactly what you want for the second code.

If I were to explain what I understood Why don't we just call print(b) once when the for statement is over?

b = []

for i in range(5):
    a = input(':')
    b.append(a)


print(b)

There is no other way to print it out, just because it's inside the for statement. If you want to print array b with all the results of the repeat statement filled, Of course, you have to write the code that you print out after the repetition.


2022-09-22 08:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.