Python学习笔记3-字符串
格式化字符串/复合字段名
>>> import humansize
>>> si_suffixes = humansize.SUFFIXES[1000]
>>> si_suffixes
['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] >>> '1000{0[0]} = 1{0[1]}'.format(si_suffixes)
'1000KB = 1MB'
>>> import humansize
>>> import sys
>>> '1MB = 1000{0.modules[humansize].SUFFIXES[1000][0]}'.format(sys)
'1MB = 1000KB'
Sys.modules 是一个保存当前python实例中搜有已导入模块的字典。模块的名字为键,模块自身为值。
>>> s = '''finished files are the re- sults of years of scientific study combined with the experience of years. ‘'' >>> s.splitlines() ['finished files are the re-', 'sults of years of scientific study combined with the', 'experience of years. ‘] >>> print(s.lower()) finished files are the re- sults of years of scientific study combined with the experience of years.
>>> a_list = query.split("&")
>>> a_list
['user=pilgrim', 'database=master', ‘password=PapayaWhip']
>>> a_list_of_list = [v.split('=',1) for v in a_list]
>>> a_list_of_list
[['user', 'pilgrim'], ['database', 'master'], ['password', ‘PapayaWhip']]
>>> a_dict = dict(a_list_of_list)
>>> a_dict
{'password': 'PapayaWhip', 'database': 'master', 'user': ‘pilgrim'}
split()-根据指定的分隔符,将字符串分隔成一个字符串列表。
dict() - 将包含列表的列表转换成字典对象
字符串的分片
>>> a_string = "My alphabet starts where your alphabet ends." >>> a_string[3:11] ‘alphabet' >>> a_string[3:-3] 'alphabet starts where your alphabet en’ >>> a_string[:18] 'My alphabet starts’ >>> a_string[18:] ' where your alphabet ends.'
String VS. Bytes
Bytes对象的定义:b’ ’, eg: by = b’abcd\x65’
Bytes对象不能改变其值,但可以通过内置函数bytearry()将bytes对象转化成bytearry对象,bytearry对象的值可改变
>>> by = b'abcd\x65' >>> barr = bytearray(by) >>> barr bytearray(b'abcde') >>> barr[0]=102 >>> barr bytearray(b'fbcde')
>>> a_string = "dive into python"
>>> by = a_string.encode('utf-8')
>>> by
b'dive into python'
>>> roundtrip = by.decode('big5')
>>> roundtrip
'dive into python'
string.encode() -- 使用某种编码方式作为参数,将字符串转化为bytes对象。
bytes.decode() -- 使用某种编码方式作为参数,将bytes对象转化成字符串对象。
Python学习笔记3-字符串的更多相关文章
- python学习笔记(字符串操作、字典操作、三级菜单实例)
字符串操作 name = "alex" print(name.capitalize()) #首字母大写 name = "my name is alex" pri ...
- Python学习笔记3—字符串
原始字符串 使用\转义或者r,这种方法在网站设置网站目录结构的时候非常管用. >>> dos="c:\news" >>> print dos c ...
- 【Python学习笔记】字符串操作
字符串的表示 python中的字符串是一个常量,可以使用单引号'',双引号""或三引号""" """来创建一个字符串常量 ...
- Python学习笔记(3)-字符串
创建字符串 一对单引号或双引号 >>> 'hello world' 'hello world' >>> "hello world" 'hello ...
- Python学习笔记:字符串
字符串 字符串定义:字符串可以使用一对单引号.双引号或三引号来定义,即便是单个字符也会当做字符串来处理(Python中没有字符类型,单个字符也就是只有一个字符的字符串而已). 原始字符串:字符串中反斜 ...
- python学习笔记(一)---字符串与列表
字符串的一些处理 字符串的大小写 name="lonmar hb" print(name.upper())#全大写 print(name.lower())#全小写 print(na ...
- 【Python学习笔记】字符串拼接方法(5种)总结
字符串的 5 种拼接方法: “+”号 “,”号 直接连接 格式化 多行字符串拼接 第一种:“+”号 print("Hello"+"Python") 打印结果: ...
- python学习笔记(二)-字符串方法
python的字符串内建函数: #====================常用方法=============================name = 'besttest' new_name = n ...
- Python学习笔记----操作字符串
1.字符串相加.列表相加.列表和字符串不能混着使用 #序列相加 a="hello" b="python" c=a+b print("字符串相加的结果& ...
- Python学习笔记之字符串
一.字符串格式化 >>> format="Hello,%s. %s enough for ya?" >>> values=('World','H ...
随机推荐
- C++ std::forward_list
std::forward_list template < class T, class Alloc = allocator > class forward_list; Forward li ...
- 深入浅出Java三大框架SSH与MVC的设计模式
现在许许多多的初学者和程序员,都在趋之若鹜地学习Web开发的宝典级框架:Struts2,Spring,Hibernate.似乎这些框架成为了一个人是否精通Java,是否会写J2EE程序的唯一事实标准和 ...
- iOS 关于PCH文件(全局文件)的介绍
1.pch文件是什么 pch文件即 扩展名为.pch的预编译文件.是将工程中较稳定的不会经常修改的代码预先编译好,放在一个公共的文件(.pch)里. 2.pch 文件的作用 A.存放宏. 比如 #d ...
- TCP同步与异步,长连接与短连接【转载】
原文地址:TCP同步与异步,长连接与短连接作者:1984346023 [转载说明:http://zjj1211.blog.51cto.com/1812544/373896 这是今天看到的一篇讲到T ...
- Android自定义控件之自定义ViewGroup实现标签云
前言: 前面几篇讲了自定义控件绘制原理Android自定义控件之基本原理(一),自定义属性Android自定义控件之自定义属性(二),自定义组合控件Android自定义控件之自定义组合控件(三),常言 ...
- 【Big Data】HADOOP集群的配置(二)
Hadoop集群的配置(二) 摘要: hadoop集群配置系列文档,是笔者在实验室真机环境实验后整理而得.以便随后工作所需,做以知识整理,另则与博客园朋友分享实验成果,因为笔者在学习初期,也遇到不少问 ...
- heart
好久没写博客了,不想废话,直接欣赏效果! 点击这里,查看完美效果! 附完整代码: <!doctype html> <html> <head> <meta ch ...
- .net请求URL过长,解决方案
<system.web> 节点下加上 <httpRuntime requestValidationMode="2.0" maxQueryStringLength= ...
- java Io文件输入输出流 复制文件
package com.hp.io; import java.io.FileInputStream; import java.io.FileNotFoundException; import java ...
- xinetd cpu 100%
今天,有个给客户试用的环境出现xinetd cpu 100%,而且连续运行很长时间了.之前也有环境发生过,今天排查解决了三四个问题,实在是查的身体都不舒服了,还没时间查这个问题... 知道的求解...