Home > Backend Development > Python Tutorial > Python实现简单截取中文字符串的方法

Python实现简单截取中文字符串的方法

WBOY
Release: 2016-06-06 11:18:49
Original
1225 people have browsed it

本文实例讲述了Python实现简单截取中文字符串的方法。分享给大家供大家参考。具体如下:

web应用难免会截取字符串的需求,Python中截取英文很容易:

>>> s = 'abce'
>>> s[0:3]
'abc'

Copy after login

但是截取utf-8的中文机会截取一半导致一些不是乱码的乱码.其实utf8截取很简单,这里记下来作为备忘

#-*- coding:utf8 -*-
s = u'中文截取'
s.decode('utf8')[0:3].encode('utf8')
# 结果u'中文截取

Copy after login

希望本文所述对大家的Python程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template