1

Say I have the following code:

myVariable = "goodmorning"

I can slice it with e.g. myVariable[1:2] (like Paolo Bergantino said here: Is there a way to substring a string in Python?), but how can I print some of the characters while jumping over others? e.g. how can I make it print "gdorn"? I tried myVariable[0,3,5,6,7] (which is wrong as it turns out) and I was thinking to myself that, if myVariable[0,3,5,6,7] worked then maybe myVariable[0,3,5:7] would work as well (I hope you see my reasoning), which it doesn't. I am learning Python through codecademy and I didn't cover functions yet, in case you go into functions and such so if there is something easier like what I tried then I would be appreciate it if you explain it!

Community
  • 1
  • 1

3 Answers3

2

You could try

myVariable = 'iheoiahwd'
idxs = [0, 3, 4, 6, 7]

myVariable = [myVariable[i] for i in idxs] 
print ''.join(myVariable)

Or simplified to a one liner:

print ''.join([myVariable[i] for i in [0, 3, 4, 6, 7]])
kezzos
  • 3,023
  • 3
  • 20
  • 37
  • ahh better than my suggestion ... :P – Joran Beasley Aug 12 '15 at 20:53
  • the letter **i**, can it be anything? And is **.join()** used for concatenating large number of things (like +, but better. just read about it here: http://stackoverflow.com/questions/1876191/explain-python-join ) and the string before **.join** if I introduce a space or whatever inside of it, will it also be printed, or the quotes mean something else here? – Dick Armstrong Aug 12 '15 at 21:03
  • When you iterate over something such as a list, at each step the letter 'i' (could be renamed to j, k, banana or whatever) would refer to the current item. ''.join basically takes a list and sticks all the items together with a seperator. In this case there is no seperator '', resulting in no gaps. If your original list had a space in it as in ' ' then the space would survive. To experiment try using ' no_way '.join(somelist) to see what happens. .join should also be much faster than using + for lists anyway – kezzos Aug 12 '15 at 21:13
  • Yes, it worked just as you said and I tried it in http://labs.codecademy.com/#:workspace. I heard that there are different versions of Python but I'll assume this will be pretty much the same. Also, my original code that made me think of this question was myBanana = "Banana", tried to make it print **Bna** (banana buddies!) – Dick Armstrong Aug 12 '15 at 21:28
0
my_indices = [1,2,5,6,9]
print "".join(x for i,x in enumerate(my_string) if i in my_indices)

is one way you could do this

you could also use numpy

print "".join(numpy.array(list(my_string))[my_indices])

this would let you do weird things like

my_indices = [1,2,3,4,9,9,8]
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0

It can be as simple as

myVariable = "goodmorning"
print (myVariable[0] + myVariable[3] + myVariable[5]  + myVariable[6] + myVariable[7] )

which outputs "gdorn".

It's not elegant. It simply builds up the string one substring at a time.

geeves
  • 652
  • 7
  • 24