python中字符串可以(且仅可以)使用成对的单引号.双引号.三个双引号(文档字符串)包围: 'this is a book' "this is a book" """this is a book""" 可在单引号包围的字符串中包含双引号,三引号等,但不能包含单引号自身(需转义) 'this is a" book' 'this is a"" book' 'this is a""…
基本字符串操作 Pyhton中字符串的格式化输出在前面已经总结了,接下来介绍一些常用的字符串操作 先定义一个字符变量,以下的操作都以此为例: name=" my name is china " #(首尾有空格) 1.首字母大写(整个字符串的首字母) print(name.capitalize()) 运行结果: my name is china 并没有变化!是因为第一个字符是空格!如果把第一个空格去掉,结果为:My name is china 2.将所有字母变大写或变小写 print(n…
# -*- coding:utf-8 -*- import sys,os txta = open('a.txt','r') str = '' for line in txta: str += line.strip().decode('utf-8') txta.close() for word in str: print word.encode('utf-8') 直接输出,是会乱码的,得先解码,再编码. 参考网址:http://blog.csdn.net/devil_2009/article/de…