• entries
73
• comments
131
• views
54710

Python #3 - Strings and Lists

99 views

I've been playing around with the String and List datatypes in python.

Strings

I must say, strings are pretty powerful especially with slicing. I know I've encountered this ability in at least one other language, but I forget which one it was. Anyway, slicing is basically a very elegant way to work with substrings.

For example, lets make a string.
>>> word = "james"

In Python, you can index each character (though Python doesn't have a char type, it's just a string of length 1) as you would in C.
>>> word[0]'j'

Slicing enables you to specify 2 indices to form a substring. Note that Python uses 0-based arrays, and it only goes up to but doesn't include the second index.
>>> word[0:5]'james'>>> word[0:2]'ja'>>> word[2:4]'me'

It has handy defaults too. If you use slicing but don't specifiy the first index, it defaults to the beginning of the string, and absence of the second index will go to the end of the string.
>>> word[:5]'james'>>> word[3:]'es'>>> word[:]'james'

This makes it very easy to form substrings! It also handles out-of-bound slices very well, as when you specify an invalid index it defaults to the beginning/end of the string. This is only with slicing though, not when using a single index into the string. You can even use negative indices and it slices the string from the right-to-left instead of left-to-right.

I am concerned about 2 things about strings though. First, the whole single/double quote thing. Why couldn't they just decide on using one character to form strings? Since you can use either single or double quotes to specify strings, it adds a little ambiguity to the language, and that's certainly not something Python is well-known for. You get into some tricky scenarios such as:
>>> 'james\''"james'">>> "\"james\'\" is cool"'"james\'" is cool'

Why can't I escape the single quote within the double quotes? I dunno, it just seems to confuse things with no benefit.

The other thing that concerns me is that you can't change strings! You can't change individual characters, or substrings. Of course you can make a whole new string, but it would be cool if you could change it like in C. I can see why you wouldn't be able to, as it just returns a temporary substring. But if you want to change one character, look what you have to do:
>>> word = 'jxmes'# word is 'jxmes'>>> word = word[:1] + "a" + word[2:]# word is now 'james'

Lists

Lists seem like another very powerful construct of Python. Just like any normal list, you can specify any number of values of any type. Lists are created with brackets and elements are comma-delimited.

Get this though. You can index into a list as if it was a sequential array.
>>> a = ['a', 1, 5+2j]>>> a[2](5+2j)

Not only that, but they can be concatenated and sliced as easily as strings. Remember how strings could be "multplied"? Lists can do that too! I wonder if a string is simply a list of characters?
>>> a[1:3][1, (5+2j)]>>> a[0:2]*2['a', 1, 'a', 1]

Actually, strings can't be a list of characters. Thank goodness that list elements are mutable.
>>> a[2] = 5>>> a['a', 1, 5]

Not only that, but list slices are mutable! It's constructs like this that make me wonder why I even use languages like C/C++. But then I think of the performance, especially for games...
>>> a = ['a', 1, 'b', 2.0]>>> a[1:3] = ['b', 5, 1.0]>>> a['a', 'b', 5, 1.0, 2.0]

You can also nest lists, of course, and there are useful functions such as "len" which return the length of the list.

That's it for strings and lists.

Recommended Comments

If you know you want to replace the first 'x' with 'a':
>>> word = word.replace( 'x'. 'a' )

And don't forget:
>> word[-1]
's'

If you want to know what else list or string have builtin:
>>> dir( s ) # or dir( str )
# Long list of member functions
>>>dir( list )
# Long list of member functions

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

• 13
• 16
• 9
• 17
• 11