0

I have a string https://www.exampleurl.com/ How would I insert a word in the middle of a string so it could look like this: https://www.subdomain.exampleulr.com/

I know I can insert the word if I did this:

url = 'https://www.exampleurl.com/'
url[:12] + 'subdomain'

It prints me https://www.subdomain, but I can't figure out how to print the rest of the string dynamically so it would adjust to the subdomain that is being appended to the string. My goal is for the end result to look like the following https://www.subdomain.exampleurl.com/

Ronald Luc
  • 1,088
  • 7
  • 17
sunflower
  • 19
  • 4

4 Answers4

2
url = 'https://www.exampleurl.com/'
content = url.split("www.")

url = content[0] + "www." + "subdomain." + content[1]
frab
  • 1,162
  • 1
  • 4
  • 14
1
url = 'https://www.exampleurl.com/'
text = url.split(".")

url =  text[0] + '.subdomain.' + text[1] + '.' + text[2]

Final output : https://www.subdomain.exampleurl.com/

Sanjay SS
  • 568
  • 4
  • 14
0

Better split on the first .:

l = url.split('.', 1)
l[0] + '.subdomain.' + l[1]

## OR if subdomain is a variable:
f'{l[0]}.{subdomain}.{l[1]}'

output: 'https://www.subdomain.exampleurl.com/'

mozway
  • 194,879
  • 13
  • 39
  • 75
0

Using replace (once)

url = 'https://www.exampleurl.com/'
url = url.replace(".", ".subdomain.", 1)   # only replaces first "." to 
                                           # get desured result
DarrylG
  • 16,732
  • 2
  • 17
  • 23