Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python substring between spaces at user defined index

Tags:

python

I am new to Python, have been watching videos on Youtube and trying to learn.

I am trying to substring a text between two spaces from a line. Let me explain, what exactly I am trying.

1) I have a text like mentioned below:

CREATE TABLE DATABASENAME.TABLENAME AS SELECT ....

2) Now from the above text I want to retrieve only DATABASENAME.TABLENAME

3) So I want to extract text between second space and third space, ie. I want to extract text between index numbered 13 and 35, but on basis on spaces.

Can anyone help? Please.

like image 760
AtulG Avatar asked Dec 05 '25 07:12

AtulG


2 Answers

string='CREATE TABLE DATABASENAME.TABLENAME AS SELECT'
extract=string.split(' ')[2]
like image 76
MaxS Avatar answered Dec 07 '25 19:12

MaxS


You can split the string into list of words based on spaces then get the elements by index in this case it's 2.

s = 'CREATE TABLE DATABASENAME.TABLENAME AS SELECT'
print(s.split(' ')) #['CREATE', 'TABLE', 'DATABASENAME.TABLENAME', 'AS', 'SELECT']
print(s.split(' ')[2]) #DATABASENAME.TABLENAME
like image 31
Chandu codes Avatar answered Dec 07 '25 19:12

Chandu codes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!