Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   I'm a new to Python (http://www.programmingforums.org/showthread.php?t=5530)

acidrock Aug 23rd, 2005 6:27 AM

I'm a new to Python
 
Hey guys, nice forums you have here.
Anyway I've started to learn python, today infact, as I've heard its a good language to learn, I have some experience in BASIC and a little in C from using Linux. I was wondering if there was a function in Python that did the same as mid$() in BASIC, I've searched for tutorials on string manipulation etc, but could not find anything on this function.
Thanks.

coldDeath Aug 23rd, 2005 6:49 AM

I have no experiance in BASIC, so please explain what the mid$() function does, then i can help you out.

coldDeath Aug 23rd, 2005 6:55 AM

Ok, i searched google and i didn't find much, from what i found i think that mid$() finds the length of a string.

In Python we have the len() function.
Example 1
:

>>> len("hello")
5

Example 2:
:

>>> variable = "hello"
>>>len(variable)
5


Moldz Aug 23rd, 2005 8:32 AM

I think the mid function returns a part of a string. In python, you can consider a string as a list of characters and use the [] notation to get slices. See this tutorial on Python string handling or some simple code:
:

>>> s = "Hello World!"
>>> s[0]
'H'
>>> s[0:5]
'Hello'
>>> s[:5]
'Hello'
>>> s[6:13]
'World!'
>>> s[6:]
'World!'
>>> s[-1]
'!'


Dietrich Aug 23rd, 2005 12:16 PM

Quote:

Originally Posted by coldDeath
I have no experiance in BASIC, so please explain what the mid$() function does, then i can help you out.

SubStr$ = MID$(MainStr$, Start% [, Length%])

returns a substring of a string
:

SubStr$ = MID$("abcdefg",3,4)
' Result: SubStr$ will equal "cdef"

SubStr$ = MID$("abcdefg",2)
' Result: SubStr$ will equal "bcdefg"


coldDeath Aug 23rd, 2005 12:27 PM

Ok, so my examples are correct to what he wants.
Thanks Dietrich for clearing that up.

acidrock Aug 23rd, 2005 4:00 PM

That's helped alot, thanks guys.


All times are GMT -5. The time now is 2:25 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC