python 按字节截取字符串
按字节截取字符串
http://www.nowcoder.com/questionTerminal/a30bbc1a0aca4c27b86dd88868de4a4a
该题目除了中文就是英文,可以判断是否为英文做区分。
注意边界条件汉子为两个字节是否会超过输出。超过输出就不加入打印字符串。
def is_Chinese(word):
for ch in word:
if '\u4e00' <= ch <= '\u9fff':
return True
return False
while:
try:
in_str =input()
num = int(input())
res = ''
c = 0
for x in in_str:
if is_Chinese(x):
c = c +2
else:
c = c +1
if c <= num :
res = res + x
else:
break
print(res)
except:
pass