473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Python substring help

Hello All,

I am trying to extract characters within quotation marks from an existing string to create a new string.

Example:

original_string = 'Hello my name is "Justin" '
desired string after string manipulation = new_string = 'Justin'

Any help would be great!

Thanks,

Justin
Sep 17 '07 #1
4 6626
bvdet
2,851 Expert Mod 2GB
Hello All,

I am trying to extract characters within quotation marks from an existing string to create a new string.

Example:

original_string = 'Hello my name is "Justin" '
desired string after string manipulation = new_string = 'Justin'

Any help would be great!

Thanks,

Justin
Using re:
Expand|Select|Wrap|Line Numbers
  1. >>> import re
  2. >>> patt = re.compile(r'\"(.+)\"')
  3. >>> s = 'Hello my name is "Justin" '
  4. >>> m = patt.search(s).group(1)
  5. >>> m
  6. 'Justin'
  7. >>> 
Sep 17 '07 #2
Using re:
Expand|Select|Wrap|Line Numbers
  1. >>> import re
  2. >>> patt = re.compile(r'\"(.+)\"')
  3. >>> s = 'Hello my name is "Justin" '
  4. >>> m = patt.search(s).group(1)
  5. >>> m
  6. 'Justin'
  7. >>> 
Works beautifully, Thank You!
Sep 17 '07 #3
bvdet
2,851 Expert Mod 2GB
Here is another application for my indexList function:
Expand|Select|Wrap|Line Numbers
  1. def indexList(s, item, i=0):
  2.     i_list = []
  3.     while True:
  4.         try:
  5.             i = s.index(item, i)
  6.             i_list.append(i)
  7.             i += 1
  8.         except:
  9.             break
  10.     return i_list
Expand|Select|Wrap|Line Numbers
  1. >>> j = indexList(s, '"')
  2. >>> s[j[0]+1:j[1]]
  3. 'Justin'
  4. >>> 
Sep 17 '07 #4
Hello All,

I am trying to extract characters within quotation marks from an existing string to create a new string.

Example:

original_string = 'Hello my name is "Justin" '
desired string after string manipulation = new_string = 'Justin'

Any help would be great!

Thanks,

Justin
Here is a third method:
Expand|Select|Wrap|Line Numbers
  1. >>> origional_string = 'Hello my name is "Justin"'
  2. >>> string_list = origional_string.split('"')
  3. >>> print string_list[1::2] # every other item starting with index 1
  4. ['Justin']
Sep 17 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

49
IDE
by: Thomas Lindgaard | last post by:
Hello I am probably going to start a war now... but so be it :) I just want to hear what all you guys who eat pythons for breakfast use for python coding. Currently I use Kate, but I would...
1
by: sysindex | last post by:
I am trying to find a way to dynamically retrieve the substring starting point of an nText field. My query looks something like SELECT ID,Substring(DOCTEXT,0,200) from mytable where DOCTEXT...
2
by: mallard134 | last post by:
Could someone please help a newbee vb programmer with a question that is driving me crazy. I am trying to understand a line of code that is supposed to return the domain portion of a valid email...
8
by: EP | last post by:
Hi, I'm a bit green in this area and wonder to what extent there may be some existing Python tools (or if I have to scratch my head real hard for an appropriate algorithm... ) I'd hate to build...
6
by: Johny | last post by:
Playing a little more with strings, I found out that string.find function provides the position of the first occurance of the substring in the string. Is there a way how to find out all...
17
by: Adam Olsen | last post by:
As was seen in another thread, there's a great deal of confusion with regard to surrogates. Most programmers assume Python's unicode type exposes only complete characters. Even CPython's own...
11
by: dyc | last post by:
how do i make use of substring method in order to extract the specified data from a a long string? I also need to do some checking b4 extracting the data, for instance: it only will extract the...
3
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Two part question: 1. Is Regex more efficient than manually comparing values using Substring? 2. I've never created a Regex expression. How would I use regex to do the equivalent of what I...
35
by: eliben | last post by:
Python provides a quite good and feature-complete exception handling mechanism for its programmers. This is good. But exceptions, like any complex construct, are difficult to use correctly,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.