Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python fixed string size

Tags:

python

string

Suppose I need a n elements long with each element seperated by a space. Further, I need the ith element to be a 1 and the rest to be a 0. What's the easiest pythonic way to do this?

Thus if n = 10, and i = 2, I would want 0 1 0 0 0 0 0 0 0 0

I want something like a new string[] that you can get in C++ but the Python version is eluding me.

Thanks!

like image 806
user1357015 Avatar asked Aug 31 '26 17:08

user1357015


1 Answers

Use a list.

n = 10
i = 2

mylist = ["0"] * n
mylist[i-1] = "1"
print " ".join(mylist)
like image 69
kindall Avatar answered Sep 02 '26 10:09

kindall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!