Mona Jalal
Mona Jalal

Reputation: 38145

splitting a string if a substring has occured

how can I say if a string hit https (with no space) split it into two words? For example in Join us!https://t.co/Fe0oTahdom want to make it like Join us! and https://t.co/Fe0oTahdom

Upvotes: 3

Views: 107

Answers (5)

rebeling
rebeling

Reputation: 728

Split and join, if https is in string.

string = "Join us!https://t.co/Fehttpsom"
if "https://" in string:
    print " https://".join(string.split("https://", 1))

Join us! https://t.co/Fehttpsom and make sure you do not split just on the first occurrence of https as in "Visit our website with https now ;)"

Upvotes: 1

MK.
MK.

Reputation: 34527

no solutions is complete without regexes!

import re

s = 'join us!https://t.co/Fe0oTahdom'
tokens = re.split('(https)', s)
print tokens[0]
print tokens[1] + tokens[2]

Upvotes: 2

iurisilvio
iurisilvio

Reputation: 4987

You can find the https with the index method.

s = 'Join us!https://t.co/Fe0oTahdom'
idx = s.index('https')
parts = [s[0:idx], s[idx:]]

Upvotes: 3

Munir
Munir

Reputation: 3612

Simplest way if you are only going to split on the https keyword

myString = 'Join us!https://t.co/Fe0oTahdom'

(head, sep, tail) = myString.partition('https')

print head  #Prints Join us!
print sep + tail #Prints the rest

Upvotes: 6

Tomasz Jakub Rup
Tomasz Jakub Rup

Reputation: 10680

Read abount index

s = 'Join us!https://t.co/Fe0oTahdom'
[s[0:s.index('https')], s[s.index('https'):]]

Upvotes: 6

Related Questions