tags:

views:

168

answers:

7

Can seem to find a substring function in python.

Say I want to output the first 100 characters in a string, how can I do this?

I want to do it safely also, meaing if the string is 50 characters it shouldn't fail.

+6  A: 
print my_string[0:100]
icktoofay
+8  A: 

Easy:

print mystring[:100]
scrible
+3  A: 
substr = string[:100]
jtbandes
+5  A: 

Slicing and dicing sequences (including strings) is an important part of Python. The following resources will come in handy:

  1. http://docs.python.org/tutorial/introduction.html
  2. http://diveintopython.org/native_data_types/lists.html
Manoj Govindan
+6  A: 

From python tutorial:

Degenerate slice indices are handled gracefully: an index that is too large is replaced by the string size, an upper bound smaller than the lower bound returns an empty string.

So it is safe to use x[:100].

czchen
+1  A: 

Slicing of arrays is done with [start:end+1].

One trick I tend to use a lot of is to indicate extra information with ellipses. So, if your field is one hundred characters, I would use:

if len(s) <= 100:
    print s
else:
    print "%s..."%(s[:97]) # Yes, I know () is superfluous, it's just my style.
paxdiablo
I guess this was meant as food for thought, but in the OP's case I would probably not suggest to do that.The result would be a string that you would have to check for content to further trim or something like that. In this case I would imagine one would either want that number to be variable, and the result to always be correct, or the number to be fixed and the handling either producing something meaningful, or error or return gracefully in case of failure.I can't think of many cases, other than delivering human readable info, where I'd want to add text arbitrarily to a string.
ThE_JacO
+1  A: 

To answer Philipp's concern ( in the comments ), slicing works ok for unicode strings too

>>> greek=u"αβγδεζηθικλμνξοπρςστυφχψω"
>>> print len(greek)
25
>>> print greek[:10]
αβγδεζηθικ

If you want to run the above code as a script, put this line at the top

# -*- coding: utf-8 -*-

If your editor doesn't save in utf-8, substitute the correct encoding

gnibbler
Not disparaging your answer, but there one only 24 letters in Greek, `ς` and `σ` are the same letter :-)
paxdiablo
@paxdiablo, doh! I copied them off the wikipedia page. Lucky I didn't name the variable `greek_alphabet` then :)
gnibbler