简体   繁体   中英

Substrings from string in python

I am very new to python and I have a basic question to make.

I have a string and I want to get some of its parts and store them as separate strings.

My string looks something like this : label = "1000 likes 200 comments 150 shares"

What I want is to have 3 string like this : likes = 1000
comments = 200 shares = 150

Thank you all in advance

You could use re.findall function.

>>> label = "1000 likes 200 comments 150 shares"
>>> likes,comments,shares = re.findall(r'\d+(?=\s+(?:likes|comments|shares)\b)', label)
>>> likes
'1000'
>>> comments
'200'
>>> shares
'150'

You can split string in words like this:

values[] = label.split()

The created variable values will look like this:

["1000","likes","200","comments","150","shares"]

Then create variables likes, comments and shares like this:

likes = values[0]
comments = values[2]
shares = values[4]

This will fill the variables with the numbers from string. See str.split() for further info.

If you want sub-strings, and the label string is always the same length and keeps the same structure you can do the following:

label =  "1000 likes 200 comments 150 shares"
likes_str = label[:10]
comments_str = label[11:23]
shares_str = label[24:]

print likes_str
print comments_str
print shares_str

It output:

1000 likes
200 comments
150 shares

Basically:
[:10] from start to char 10
[11:23] from char 11 to 23
[24:] from char 24 to end

Check previous stackoverflow question and this tutorial on strings too

If you want to extract parts of a string, you can do it in a very basic way getting just the positions of each character.

Therefore if you know the positions that you want to extract, you can create new strings getting only the characters that you want.

You can index the string and do something like that:

label = "1000 likes 200 comments 150 shares"
#Likes gets from character in the position 0 until the character in the position 3
likes = label[0:4]
#comments gets from character in the position 11 until the character in the position 13
comments = label[11:14]
#shares gets from character in the position 24 until the character in the position 26
shares =  label[24:27]

After that you have the three strings

You may want a more general solution to your problem, if you don't know exactly the quantity if items in the string (for Python 3.3):

# import regular expressions module
import re
label = "1000 likes 200 comments 150 shares 4 edits"
# split all words in your string
labelList = re.split('\s+', label)
# count quantity of items in your string
quantOfItems = int(len(labelList) / 2)
# iterate all items from 0 to quantOfItems
for i in range(quantOfItems):
    # output the result
    print('%s = %s' % (labelList[i*2+1], labelList[i*2]))

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