1

im triying to print horizontally

Text="Si ya conoces Colab, echa un vistazo a este vídeo para obtener información sobre las tablas interactivas"
List= [31,43,15,5,26,32,6,1,3,62,62,62,62,62,62]

for x in List:
  print (Text[x])

Result

s

o

u

t

c

i

y

And i Wanted

s o u t c i y

nor do I want to do it like this:

print(Text[31],Text[43],Text[15],Text[5],Text[26],Text[32],Text[6],Text[1],Text[3],Text[62],Text[62],Text[62],Text[62],Text[62],Text[62])
CodeLikeBeaker
  • 20,682
  • 14
  • 79
  • 108
Dami Devil
  • 67
  • 3

5 Answers5

1

In python, strings are natively indexable. For example:

>>> Text = "Si ya conoces Colab, echa un vistazo a este vídeo para obtener información sobre las tablas interactivas"
>>> Text[0]
'S'
>>> Text[1]
'i'

With that ability, we can do a list comprehension that iterates your List and returns the value of the index in Text if the value is not a space. This returns a list where each value is the right letter. We then join that list into a string using a space character to separate each letter.

>>> Text = "Si ya conoces Colab, echa un vistazo a este vídeo para obtener información sobre las tablas interactivas"
>>> List = [31,43,15,5,26,32,6,1,3,62,62,62,62,62,62]
>>> print(' '.join([Text[x] for x in List if Text[x] != " "]))
s o u t c i y

The list comprehension above is nearly equivalent to this longer bit of code:

>>> new_list = []
>>> for x in List:
...     if Text[x] != ' ':
...         new_list.append(Text[x])
...
>>> print(" ".join(new_list))
s o u t c i y
danielhoherd
  • 481
  • 2
  • 5
1

You could also use the sep keyword arg for print, and expand a generator expression into print.

print(*(Text[x] for x in List if Text[x] != ' '), sep=" ", end="")
Chris
  • 26,361
  • 5
  • 21
  • 42
0

This should work, also would do a check to see if the index is in range.

for x in l:
 if x < len(text):
    print(text[x], end = " ")
fynmnx
  • 571
  • 6
  • 29
0

A good way would be to add it to a string first, and then print it out

text="Si ya conoces Colab, echa un vistazo a este vídeo para obtener información sobre las tablas interactivas"
list= [31,43,15,5,26,32,6,1,3,62,62,62,62,62,62]

output = "" #initialize the output string
for x in list: 
  #continually concat to the output string
  output += text[x]

print(output)

sir_
  • 46
  • 4
0

try this:

for x in List:
    print (Text[x],end="")
MatthewRBevins
  • 184
  • 1
  • 2
  • 6