格式化字符串/复合字段名

>>> 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-字符串的更多相关文章

  1. python学习笔记(字符串操作、字典操作、三级菜单实例)

    字符串操作 name = "alex" print(name.capitalize()) #首字母大写 name = "my name is alex" pri ...

  2. Python学习笔记3—字符串

    原始字符串 使用\转义或者r,这种方法在网站设置网站目录结构的时候非常管用. >>> dos="c:\news" >>> print dos c ...

  3. 【Python学习笔记】字符串操作

    字符串的表示 python中的字符串是一个常量,可以使用单引号'',双引号""或三引号""" """来创建一个字符串常量 ...

  4. Python学习笔记(3)-字符串

    创建字符串 一对单引号或双引号 >>> 'hello world' 'hello world' >>> "hello world" 'hello ...

  5. Python学习笔记:字符串

    字符串 字符串定义:字符串可以使用一对单引号.双引号或三引号来定义,即便是单个字符也会当做字符串来处理(Python中没有字符类型,单个字符也就是只有一个字符的字符串而已). 原始字符串:字符串中反斜 ...

  6. python学习笔记(一)---字符串与列表

    字符串的一些处理 字符串的大小写 name="lonmar hb" print(name.upper())#全大写 print(name.lower())#全小写 print(na ...

  7. 【Python学习笔记】字符串拼接方法(5种)总结

    字符串的 5 种拼接方法: “+”号 “,”号 直接连接 格式化 多行字符串拼接 第一种:“+”号 print("Hello"+"Python") 打印结果: ...

  8. python学习笔记(二)-字符串方法

    python的字符串内建函数: #====================常用方法=============================name = 'besttest' new_name = n ...

  9. Python学习笔记----操作字符串

    1.字符串相加.列表相加.列表和字符串不能混着使用 #序列相加 a="hello" b="python" c=a+b print("字符串相加的结果& ...

  10. Python学习笔记之字符串

    一.字符串格式化 >>> format="Hello,%s. %s enough for ya?" >>> values=('World','H ...

随机推荐

  1. 详解mmseg

    本文先介绍下mmseg的概念和算法,再说下mmseg4j-solor的3个分词器用法 1.mmseg概念 mmseg是用于中文切词的算法,即Maximum Matching Segment,最大匹配分 ...

  2. Javascript之匿名函数

    分析: 1.所谓匿名函数,从字面意思理解,就是没有名字的函数,js 用()来代替(注意,是英文状态下的括号) 2.定义形式: function (){ //to add codes that you ...

  3. jQuery +ajax +json+实现分页

    正文 首先我们创建一般处理程序,来读取数据库中内容,得到返回值. 创建文件,GetData.ashx. 我这里是用的存储过程,存储过程会再下面粘出来,至于数据只是实例,你们可根据需求自行读取数据 代码 ...

  4. Electron的环境配置

    原文地址http://huisky.com/blog/161218121551123 本文介绍了Electron的环境配置,包括Electron下载.nodejs下载安装.NPM+Bower安装配置. ...

  5. SSIS 属性:ExecValueVariable

    有些Task组件执行完成之后,会产生输出结果,称作Execution Value,例如,Execute SQL Task在执行完成之后,会返回受影响的数据行数.Task组件的Execution Val ...

  6. 【Win 10 应用开发】导入.pfx证书

    这个功能其实并不常用,一般开发较少涉及到证书,不过,简单了解一下还是有必要的. 先来说说制作测试证书的方法,这里老周讲两种方法,可以生成用于测试的.pfx文件. 产生证书,大家都知道有个makecer ...

  7. 判断一个值是否为null或者undefined

    var a=null; var b=undefined; if(a===null){ //a==null alert("a=null") }else{ alert("a= ...

  8. PHP资源列表

    一个PHP资源列表,内容包括:库.框架.模板.安全.代码分析.日志.第三方库.配置工具.Web 工具.书籍.电子书.经典博文等等. 初始翻译信息来自:<推荐!国外程序员整理的 PHP 资源大全& ...

  9. C#控制台程序取得INSOYA视频区的视频的真实URL,视频标题,发布时间集合。

    准备工作 起因是因为这个网站:http://i.youku.com/kmsfan 这个是一个叫做冒险岛的游戏的资讯论坛,以前我经常在里面传视频,现在我不玩这个游戏了,但是很多玩家还是经常到我的网站里面 ...

  10. ASP.NET Core 中文文档 第三章 原理(13)管理应用程序状态

    原文:Managing Application State 作者:Steve Smith 翻译:姚阿勇(Dr.Yao) 校对:高嵩 在 ASP.NET Core 中,有多种途径可以对应用程序的状态进行 ...