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

Search for letters in a String [solved]

How do i search for one letter in a String? For example the third letter in Word="Hello". The thought is that i that way can use:

if (the third letter in Word)=="l":

Thanks!
Oct 30 '06 #1
4 1583
bvdet
2,851 Expert Mod 2GB
How do i search for one letter in a String? For example the third letter in Word="Hello". The thought is that i that way can use:

if (the third letter in Word)=="l":

Thanks!
Try this:
Expand|Select|Wrap|Line Numbers
  1. if string.index(some_string, some_sub_string) == 2:
  2.        .. do_something..
  3.  
Oct 30 '06 #2
bartonc
6,596 Expert 4TB
I'd say don't forget to import string, BUT 2.4 docs say:
4.1.4 Deprecated string functions

index(s, sub[, start[, end]])
Like find() but raise ValueError when the substring is not found.
So, use:
Expand|Select|Wrap|Line Numbers
  1. someStr.index(subStr)
  2.  
but that needs to be wrapped in a try block. So use
Expand|Select|Wrap|Line Numbers
  1. index = someStr.find(subStr)
  2. if index != -1:
  3.     # do something knowing someStr[index] == subStr if subStr is on character.
  4.  
Try this:
Expand|Select|Wrap|Line Numbers
  1. if string.index(some_string, some_sub_string) == 2:
  2. .. do_something..
  3.  
Oct 30 '06 #3
Geos
2
maybe this is just to obvious but if you only want to test the third letter of a string named word try this:

Expand|Select|Wrap|Line Numbers
  1. if word[2] == 'l':
  2.     ...
  3.  
Oct 31 '06 #4
Thanks... hehe... actually it was the most obvious one i was looking for...
Oct 31 '06 #5

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....
10
by: Case Nelson | last post by:
Hi there I've just been playing around with some python code and I've got a fun little optimization problem I could use some help with. Basically, the program needs to take in a random list of no...
3
by: Girish Sahani | last post by:
Given a length k string,i want to search for 2 substrings (overlap possible) in a list consisting of length k-1 strings. These 2 substrings when 'united' give the original string. e.g given...
5
by: DFS | last post by:
This works pretty well, and it's easy, but it's not the ultimate solution. The kludgey part is it uses a hidden field to incrementally capture the keystrokes in the visible field (because executing...
3
by: Harry Haller | last post by:
What is the fastest way to search a client-side database? I have about 60-65 kb of data downloaded to the client which is present in 3 dynamically created list boxes. The boxes are filled from 3...
11
by: Ola K | last post by:
Hi, I am pretty new to Python and I want to make a script that will search for the following options: 1) words made of uppercase characters -only- (like "YES") 2) words made of lowercase...
4
by: BenCoo | last post by:
Hello, In a Binary Search Tree I get the error : Object must be of type String if I run the form only with the "Dim bstLidnummer As New BinarySearchTree" it works fine. Thanks for any...
1
by: lankansoft | last post by:
I have added full text search to my website recently. The searching facility is working fine and the search string should have at least 4 letters. Now I need the search engine to work on strings...
8
by: NMarks | last post by:
Hello all, I have created a database for my work that stores information for keys/locks, doors and employees. Specifically the database contains all the information of our lock system, which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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.