473,320 Members | 1,910 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,320 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 6624
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.