473,396 Members | 1,914 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,396 software developers and data experts.

RE: Testing for the first few letters of a string

use re module

import re
template = '^My name is alex'
astring = 'My name is alex, and I like pie'
if re.match(template, astring):
print 'Found it'
else: print '%s does not begin with %s' % (astring, template)

good luck.
Edwin

-----Original Message-----
From: py************************************************ **@python.org
[mailto:py***************************************** *********@python.org]
On Behalf Of Alexnb
Sent: Thursday, August 07, 2008 11:40 AM
To: py*********@python.org
Subject: Testing for the first few letters of a string

Okay, I have a fix for this problem, but it is messy and I think there might
be a better way. Heres an example:

Lets say I have a string: "My name is alex"

and I have another string "My name is alex, and I like pie".

I want to test to see if just the "My name is alex" part is there. I don't
care about the pie part.
My first instinct was to just create a for loop and test for the string like
this:

n = 0

for x in string1:
if string1[n] == string2[n]
n = n +0
else:
break
and then later testing to see what n was = to and figuring out if it got
through the whole loop. I feel like there should be an easier way to do
this, and probably is. So Does anyone have a suggestion?
--
View this message in context: http://www.nabble.com/Testing-for-th...p18873375.html
Sent from the Python - python-list mailing list archive at Nabble.com.

--
http://mail.python.org/mailman/listinfo/python-list
The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure. If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof. Thank you.
Aug 7 '08 #1
2 1282
On Aug 8, 1:53 am, Edwin.Mad...@VerizonWireless.com wrote:
use re module
Using re.match instead of str.startswith is overkill.
>
import re
template = '^My name is alex'
The ^ is redundant.
astring = 'My name is alex, and I like pie'
if re.match(template, astring):
print 'Found it'
else: print '%s does not begin with %s' % (astring, template)
Aug 7 '08 #2
John Machin wrote:
>import re
template = '^My name is alex'

The ^ is redundant.
Didn't the OP want to match only at the beginning of the string?

m.
Aug 7 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Edward K. Ream | last post by:
From the documentation for the string module at: C:\Python23\Doc\Python-Docs-2.3.1\lib\module-string.html letters: The concatenation of the strings lowercase and uppercase described below....
2
by: colm | last post by:
I have a string surname and want to select the first two letters to use in a code field how can i check if the second item in string is an apostrophe and filter it out ' in names such as o'brien...
3
by: snow.carriers | last post by:
Let me first state that I'm using Borland Turbo C++, it's relatively old so the new string methods won't work. Anyways, first I'm trying to collect a line of a string (with numbers, letters,...
13
by: Pedro Graca | last post by:
I'm sure this isn't very pythonic; comments and advice appreciated def curious(text): """ Return the words in input text scrambled except for the first and last letter. """ new_text = "" word...
6
by: cman | last post by:
If you have the following string variable declared: char *current; What does the following test in a while-loop amount to: While (!*current) Does this check if *current is "not empty" i.e...
11
by: VK | last post by:
In the continuation of the discussion at "Making Site Opaque -- This Strategy Feasible?" and my comment at http://groups.google.com/group/comp.lang.javascript/msg/b515a4408680e8e2 I have...
1
by: Alexnb | last post by:
Okay, I have a fix for this problem, but it is messy and I think there might be a better way. Heres an example: Lets say I have a string: "My name is alex" and I have another string "My name...
0
by: Jean-Paul Calderone | last post by:
On Thu, 07 Aug 2008 23:08:10 +0200, magloca <magloca@mailinater.comwrote: That's why it's "redundant" instead of "incorrect". ;) re.match = match(pattern, string, flags=0) Try to apply the...
2
by: whodgson | last post by:
The following code accepts a string from the user and swaps the first and last letters in each word. It prints out the original string incorrectly by dropping off the first letter of the first word....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.