471,348 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 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 6444
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

Post your reply

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

Similar topics

IDE
49 posts views Thread by Thomas Lindgaard | last post: by
1 post views Thread by sysindex | last post: by
6 posts views Thread by Johny | last post: by
17 posts views Thread by Adam Olsen | last post: by
11 posts views Thread by dyc | last post: by
3 posts views Thread by =?Utf-8?B?anAybXNmdA==?= | last post: by
35 posts views Thread by eliben | last post: by
reply views Thread by Ronak mishra | last post: by

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.