问题:输出字符串中的前100个字符

似乎可以在python中找到子字符串函数。

假设我要输出字符串中的前100个字符,该怎么办?

我也想安全地进行操作,测量字符串是否为50个字符,它应该不会失败。

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.


回答 0

print my_string[0:100]
print my_string[0:100]

回答 1

python教程

退化的切片索引得到了很好的处理:太大的索引将替换为字符串大小,上限小于下限的字符串将返回一个空字符串。

因此使用安全x[:100]

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


回答 2

简单:

print mystring[:100]

Easy:

print mystring[:100]

回答 3

为了回答Philipp的担忧(在评论中),对于unicode字符串也可以切片

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

如果您想将以上代码作为脚本运行,请将此行放在顶部

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

如果您的编辑器未保存为utf-8,请替换为正确的编码

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


回答 4

数组的切片是通过进行的[first:last+1]

我倾向于使用很多技巧之一是用省略号指示额外的信息。因此,如果您的字段是一百个字符,我将使用:

if len(s) <= 100:
    print s
else:
    print "%s..."%(s[:97])

是的,我知道()在这种情况下对于%格式化运算符是多余的,这只是我的风格。

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.


回答 5

如果您的字符串不够长,大多数前面的示例都会引发异常。

另一种方法是使用 'yourstring'.ljust(100)[:100].strip()

这将给您前100个字符。如果您的字符串的最后符为空格,则可能会得到较短的字符串。

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.


回答 6

使用字符串格式化%是解决此问题的好方法。这里有些例子。

格式代码'%s'将转换'12345'为字符串,但是已经是字符串。

>>> '%s' % '12345'

'12345'

'%.3s' 指定仅使用前三个字符。

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

'123'

'%.7s'说要使用前七个字符,但只有五个。没问题。

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

'12345'

'%7s' 最多使用七个字符,用左侧的空格填充缺少的字符。

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

'  12345'

'%-7s' 是一样的事情,除了在右边填充丢失的字符。

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

'12345  '

'%5.3' 表示使用前三个字符,但在左侧填充空格以总计五个字符。

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

'  123'

除了在右边填充之外,其他内容相同。

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

'123  '

也可以处理多个参数!

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

'do u no 123 da 67 wae'

如果您需要更大的灵活性,str.format()也可以使用。这是两者的文档

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.


回答 7

[start:stop:step]

因此,如果您只想使用100个第一个字符,请使用your_string[0:100]your_string[:100] 如果只想在偶数位置使用your_string[::2] 该字符,请使用“默认值”,以start为0,stop-字符串len,以及step-1。当您不提供其中之一并在其中加上“:”时,它将使用其默认值。

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


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。