Python 输出字符串的前 100 个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3486384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Output first 100 characters in a string
提问by Blankman
Can seem to find a substring function in python.
似乎可以在 python 中找到一个子字符串函数。
Say I want to output the first 100 characters in a string, how can I do this?
假设我想输出字符串中的前 100 个字符,我该怎么做?
I want to do it safely also, meaing if the string is 50 characters it shouldn't fail.
我也想安全地做到这一点,意思是如果字符串是 50 个字符,它就不应该失败。
采纳答案by icktoofay
print my_string[0:100]
回答by Arkady
Easy:
简单:
print mystring[:100]
回答by czchen
From python tutorial:
来自python 教程:
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.
退化切片索引被优雅地处理:太大的索引被替换为字符串 size,小于下限的上限返回一个空字符串。
So it is safe to use x[:100].
所以使用起来是安全的x[:100]。
回答by paxdiablo
Slicing of arrays is done with [first:last+1].
数组的切片是用[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.
是的,我知道()在这种情况下对于%格式化操作符来说是多余的,这只是我的风格。
回答by John La Rooy
To answer Philipp's concern ( in the comments ), slicing works ok for unicode strings too
为了回答 Philipp 的担忧(在评论中),切片也适用于 unicode 字符串
>>> 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
如果您的编辑器没有以 utf-8 格式保存,请替换正确的编码
回答by Julien Kieffer
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().
另一种方法是使用
'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.
这将为您提供前 100 个字符。如果您的字符串最后一个字符是空格,您可能会得到一个较短的字符串。
回答by OrangeSherbet
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'为字符串,但它已经是字符串。
>>> '%s' % '12345'
'12345'
'%.3s'specifies to use only the first three characters.
'%.3s'指定仅使用前三个字符。
>>> '%.3s' % '12345'
'123'
'%.7s'says to use the first seven characters, but there are only five. No problem.
'%.7s'说使用前七个字符,但只有五个。没问题。
>>> '%.7s' % '12345'
'12345'
'%7s'uses up to seven characters, filling missing characters with spaces on the left.
'%7s'最多使用七个字符,用左边的空格填充缺失的字符。
>>> '%7s' % '12345'
' 12345'
'%-7s'is the same thing, except filling missing characters on the right.
'%-7s'是一样的,除了在右边填充缺失的字符。
>>> '%-7s' % '12345'
'12345 '
'%5.3'says use the first three characters, but fill it with spaces on the left to total five characters.
'%5.3'说使用前三个字符,但用左边的空格填充它总共五个字符。
>>> '%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 documentationfor both.
回答by Szymek G
[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.
因此,如果您只想取 100 个第一个字符,请使用your_string[0:100]或your_string[:100]如果您只想取偶数位置的字符,请使用your_string[::2]start 的“默认值”为 0,停止 - 字符串的 len,以及步骤 - 1。所以当您不提供其中之一并放置“:”时,它将使用它的默认值。

