Output first 100 characters in a string

Question:

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, meaning if the string is 50 characters it shouldn’t fail.

Asked By: Blankman

||

Answers:

print my_string[0:100]
Answered By: icktoofay

Easy:

print mystring[:100]
Answered By: Arkady

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].

Answered By: czchen

Slicing of arrays is done with [first:last+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])

And yes, I know () is superfluous in this case for the % formatting operator, it’s just my style.

Answered By: paxdiablo

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

Answered By: John La Rooy

Most of previous examples will raise an exception in case your string is not long enough.

Another approach is to use
'yourstring'.ljust(100)[:100].strip().

This will give you first 100 chars.
You might get a shorter string in case your string last chars are spaces.

Answered By: Julien Kieffer

String formatting using % is a great way to handle this. Here are some examples.

The formatting code '%s' converts '12345' to a string, but it’s already a string.

>>> '%s' % '12345'

'12345'

'%.3s' specifies to use only the first three characters.

>>> '%.3s' % '12345'

'123'

'%.7s' says to use the first seven characters, but there are only five. No problem.

>>> '%.7s' % '12345'

'12345'

'%7s' uses up to seven characters, filling missing characters with spaces on the left.

>>> '%7s' % '12345'

'  12345'

'%-7s' is the same thing, except filling missing characters on the right.

>>> '%-7s' % '12345'

'12345  '

'%5.3' says use the first three characters, but fill it with spaces on the left to total five characters.

>>> '%5.3s' % '12345'

'  123'

Same thing except filling on the right.

>>> '%-5.3s' % '12345'

'123  '

Can handle multiple arguments too!

>>> 'do u no %-4.3sda%3.2s wae' % ('12345', 6789)

'do u no 123 da 67 wae'

If you require even more flexibility, str.format() is available too. Here is documentation for both.

Answered By: JoseOrtiz3
[start:stop:step]

So If you want to take only 100 first character, use your_string[0:100] or your_string[:100]
If you want to take only the character at even position, use your_string[::2]
The “default values” for start is 0, for stop – len of string, and for step – 1. So when you don’t provide one of its and put ‘:’, it’ll use it default value.

Answered By: Szymek G
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.