1

I'm confused about this code! Why # cant't play a role that takes the length of a string?

 string="abcd"

 !echo ${#string}

In fact, the code behind # has become commented and cannot be executed!

Any advice?

Jack Zhang
  • 11
  • 1

1 Answers1

1

This works correctly, but you cannot mix python and bash variables in this way. Try this instead:

!string="abcd" && echo ${#string}

The two statements have to be on the same line because in IPython, each ! statement opens a temporary subshell and variables are not persisted between shells. If you want to use multiline bash programs, you can use the %%bash cell magic instead:

%%bash
string="abcd"
echo  ${#string}
jakevdp
  • 77,104
  • 11
  • 125
  • 160