简体   繁体   中英

Breaking 1 String into 2 Strings based on special characters using python

I am working with python and I am new to it. I am looking for a way to take a string and split it into two smaller strings. An example of the string is below

wholeString = '102..109'

And what I am trying to get is:

a = '102'
b = '109'

The information will always be separated by two periods like shown above, but the number of characters before and after can range anywhere from 1 - 10 characters in length. I am writing a loop that counts characters before and after the periods and then makes a slice based on those counts, but I was wondering if there was a more elegant way that someone knew about. Thanks!

Try this:

a, b = wholeString.split('..')

It'll put each value into the corresponding variables.

看看string.split方法。

split_up = [s.strip() for s in wholeString.split("..")]

This code will also strip off leading and trailing whitespace so you are just left with the values you are looking for. split_up will be a list of these values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM