Home > Blockchain >  How to add strings before and after a string?
How to add strings before and after a string?

Time:01-27

I would like to add some characters to the text output.


Here is my code:

thumbnailname = detail.xpath('.//h1/text()').get().replace(' ','-')

Here is my text output:

Apple-iPhone-13-Pro-Max


How do I get the following output?

My-Apple-iPhone-13-Pro-Max.jpg


CodePudding user response:

Just concatenate the strings:

thumbnailname = "My-"   detail.xpath('.//h1/text()').get().replace(' ','-')   ".jpg"

CodePudding user response:

you have 2 easy ways to add strings before and after your string:

  1. yourstring = "start{0}end".format(yourstring)

  2. yourstring = "start" yourstring "end"

in your case:

thumbnailname = detail.xpath('.//h1/text()').get().replace(' ','-')
thumbnailname = "My-"   thumbnailname   ".jpg"

or

thumbnailname = "My-{0}.jpg".format(detail.xpath('.//h1/text()').get().replace(' ','-'))"
  •  Tags:  
  • Related