3

I don't know how to express this. I want to print:

_1__2__3__4_

With "_%s_" as a substring of that. How to get the main string when I format the substring? (as a shortcut of:

for x in range(1,5):
    print "_%s_" % (x)

(Even though this prints multiple lines))

Edit: just in one line

2
  • Do you mean something like "_" + s + "_" Commented Aug 25, 2014 at 20:12
  • I think something like this is exactly what he's looking for... Just adding a string onto another in every loop iteration. Commented Aug 25, 2014 at 20:13

4 Answers 4

9

Did you mean something like this?

 my_string = "".join(["_%d_" % i for i in xrange(1,5)])

That creates a list of the substrings as requested and then concatenates the items in the list using the empty string as separator (See str.join() documentation).

Alternatively you can add to a string though a loop with the += operator although it is much slower and less efficient:

s = ""
for x in range(1,5):
    s += "_%d_" % x
print s
Sign up to request clarification or add additional context in comments.

4 Comments

Yeah! I exactly saw that: "_%s_" % i for i in range(1,5). Why do u use join to empty string?
@wonderwhy join is a method on the str type - so you have to use it on some string. It's often used like ', '.join(items) -> foo, bar, baz as a very elegant way to deal with the fencepost problem, but just as well can be used with the empty string for easy way to concatenate items of a sequence.
Ah yea, I tried without join to "" and it just gave an array. Anyway the logic I saw some time ago and I was looking forward to seeing is that.
@LukasGraf hmm, I didn't know about the fencepost stuff till now, that's interesting. I didn't realize about that if I hadn't got that error :) Cool method.
2
print("_" + "__".join(map(str, xrange(1,5)))) +"_"
_1__2__3__4_




In [9]: timeit ("_" + "__".join(map(str,xrange(1,5)))) +"_"

1000000 loops, best of 3: 1.38 µs per loop


In [10]: timeit "".join(["_%d_" % i for i in xrange(1,5)])
100000 loops, best of 3: 3.19 µs per loop

1 Comment

This works great, but I was looking for the logic @PaulSeeb is showing in his first example, since he formats the string inside a loop.
1

you can maintain your style if you want to.

if you are using python 2.7:

from __future__ import print_function
for x in range(1,5):
    print("_%s_" % (x), sep = '', end = '')
print()

for python 3.x, import is not required.

python doc: https://docs.python.org/2.7/library/functions.html?highlight=print#print

1 Comment

That's a cool answer, I didn't know about that way to push to queue and then print altogether. That's not what I was looking for but is a very interesting answer. Thanks for sharing that.
1

Python 3:

print("_{}_".format("__".join(map(str,range(1,5)))))
_1__2__3__4_

Python 2:

print "_{0}_".format("__".join(map(str,range(1,5))))
_1__2__3__4_

1 Comment

this is an alternative to paul seed's answer. Very interesting too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.