Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > String functions

Reply
Thread Tools

String functions

 
 
Kali K E
Guest
Posts: n/a
 
      09-13-2003
Hi,

I want two simple things.

1. To find the length of the string
2. To find the contants of the last character in a string. I tried to
use $ for that, but I am getting syntax error. I do not know the
correct format. Basically I have to check
if mystring[lastchr] == '.':
dosomething
else:
donothing

Please help me.
 
Reply With Quote
 
 
 
 
Jordan Krushen
Guest
Posts: n/a
 
      09-13-2003
On 13 Sep 2003 12:21:30 -0700, Kali K E <(E-Mail Removed)> wrote:

> Hi,
> I want two simple things.
>
> 1. To find the length of the string


>>> str = 'i am a string'
>>> len(str)

13

> 2. To find the contants of the last character in a string.


>>> str[-1]

'g'

> Basically I have to check
> if mystring[lastchr] == '.':
> dosomething
> else:
> donothing


if str[-1] == '.':
dosomething
else:
donothing

HTH,

J.
 
Reply With Quote
 
 
 
 
Chris Tavares
Guest
Posts: n/a
 
      09-13-2003
"Kali K E" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Hi,
>
> I want two simple things.
>
> 1. To find the length of the string


Use the len function:

>>> s = "Hi there!"
>>> print s

Hi there!
>>> len(s)

9
>>>


> 2. To find the contants of the last character in a string. I tried to
> use $ for that, but I am getting syntax error. I do not know the
> correct format. Basically I have to check
> if mystring[lastchr] == '.':
> dosomething
> else:
> donothing
>
> Please help me.


Use the indexer. Python sequences (lists, tuples, strings) can take negative
numbers as indexes. If you use a negative number, it starts counting from
the end of the sequence. So, to get the last character, do:

>>> print s[-1]

!
>>>


-Chris



 
Reply With Quote
 
Klaus Alexander Seistrup
Guest
Posts: n/a
 
      09-13-2003
Kali K E wrote:

> if mystring[lastchr] == '.':
> dosomething
> else:
> donothing


#v+

>>> s = 'abc'
>>> s.endswith('c')

True
>>> s.endswith('z')

False
>>>


#v-

// Klaus

--
><> unselfish actions pay back better

 
Reply With Quote
 
Scott David Daniels
Guest
Posts: n/a
 
      09-14-2003
Jordan Krushen wrote:
> On 13 Sep 2003 12:21:30 -0700, Kali K E <(E-Mail Removed)> wrote:
>>... Basically I have to check
>> if mystring[lastchr] == '.':
>> dosomething
>> else:
>> donothing

> if str[-1] == '.':
> dosomething
> else:
> donothing


The idiom you want is probably:
if somestr.endswith('.'):
dosomething
else:
donothing

(1) don't use str as a variable name, you'll mask the builtin str.
(2) the above code raises no exception if somestr is length 0.
''[-1] raises an exception, but ''.endswith('whatever') is False.

-Scott David Daniels
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using the string functions (ex. find()) on a multi-symbol string korean_dave Python 2 06-17-2008 10:12 PM
searching an array, string compare functions, string sorting ckirchho@directmedia.de Javascript 2 10-10-2007 11:23 PM
String functions run on empty string Tim Slattery ASP General 1 07-11-2007 06:10 PM
Shared functions vs Non-Shared Functions tshad ASP .Net 11 05-27-2005 05:53 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM



Advertisments