1

So I am fairly new to python and am only self taught in this particular language, but I have hit a bit of a snag.

What I am trying to do is build a string that has with in it a digit that can be of any length or pattern. For example:

   "Data_image_%s.%d" %(myStr, r'[0-9]+') 
   # Well, this won't actually work since %d is expecting an integer,
   # but you get the idea.

When I do this I get back

Data_image_picture.[0-9]+.png

The ultimate objective is to create a string that reads like: Data_image_picture.1234567.png

Any thoughts on how I might go about correcting this?

Edit: What I am trying to do is take images off my companies website but the image url is dynamic depending on who and when the images are loaded to the browser. So the format is typically "data_image_Joe_session#id.png

Edit 2: I think I have been going about this problem the wrong way, I'll likely need to parse through the data to figure what string patterns I need, instead of creating a one size fits all string like I have been doing.

Thank you for your time and help

4
  • You wanna bulk create strings on that shape? Like from Data_image_picture.0.png to Data_image_picture.9223372036854775807.png ? Commented Dec 3, 2014 at 22:41
  • What I am trying to do is take images off my companies website but the image url is dynamic depending on who and when the images are load to the browser. so the format is typically "data_image_Joe_session#id.png Commented Dec 3, 2014 at 22:48
  • Do you know that id before trying to generate the Data_image_picture...blahblah? I mean... Are you gonna know what number you need to add to it? If that's the case, what @TanveerAlam or @Hackaholic posted below should be good. Commented Dec 3, 2014 at 22:50
  • You may possibly be wanting to use the re module. Commented Dec 3, 2014 at 22:52

2 Answers 2

1

Regular expressions are used for matching, not for building strings or building random numbers.

To build a string with a random number between 0 and 9999999 you could do something like:

from random import random

myStr = "Data_image_picture." + str(int(random()*10000000)) + ".png"
Sign up to request clarification or add additional context in comments.

Comments

1

%d is used as a placeholder for numeric or decimal values.

They are format specifiers. They are used when you want to include the value of your Python expressions into strings, with a specific format enforced.

So %d accepts integer but place it inside string.

>>> image_name = "picture"
>>> image_number = 1234567
>>> "Data_image_%s.%d.jpg"%(image_name,image_number)
'Data_image_picture.1234567.jpg'
#OR
>>> "Data_image_{}.{}.jpg".format(image_name,image_number)
'Data_image_picture.1234567.jpg'

You can check the type of the number that is formated inside string:

>>> type(image_number)
<type 'int'>

You can take number of any lenght.

>>> image_number = 123456789234567
>>> image_name = "picture"
>>> "Data_image_%s.%d.jpg"%(image_name,image_number)
'Data_image_picture.123456789234567.jpg'
>>> type(image_number)
<type 'long'>

This is in Python 2 but in Python 3 integer can hold up long numbers.

In Python3:

image_number = 123456789234567
image_name = "picture"
print ("Data_image_%s.%d.jpg"%(image_name,image_number))
print (type(image_number))

Results :

Data_image_picture.123456789234567.jpg
<class 'int'>

2 Comments

I think I have been going about this problem the wrong way, I'll likely need to parse through the data to figure what string patterns I need.
@Riv3r Yes i think you just got confused because you thought you are trying to include integer with string.

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.