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

>>> 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. The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files.

    参照 http://stackoverflow.com/questions/24301986/the-type-java-lang-charsequence-cannot-be-resolved-in ...

  2. hibernate与Struts框架结合编写简单针对修改练习

    失败页面fail.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  3. SharedPreferences 的另一种场景的用法

    SharedPreferences 的另一种场景的用法 昨天,下班在家想做什么来着,然后想用SharedPreferences存点数据,但是不知道咋地突然想到,SharedPreferences是应用 ...

  4. 【.net 深呼吸】EqualityComparer——自定义相等比较

    自定义实现两个对象的相等比较,一种方案是重写Object类的Equals方法,很easy,如果相等返回true,不相等就返回false.不过,如果把自定义相等的比较用于泛型集,比如Dictionary ...

  5. LBaaS 实现机制 - 每天5分钟玩转 OpenStack(125)

    上一节我们已经配置并测试 LBaaS,今天重点分析 Neutron 是如何用 Haproxy 来实现负责均衡的. 在控制节点上运行 ip netns,我们发现 Neutron 创建了新的 namesp ...

  6. 从零开始编写自己的C#框架(20)——框架异常处理及日志记录

    最近很忙,杂事也多,所以开发本框架也是断断续续的,终于在前两天将前面设定的功能都基本完成了,剩下一些小功能遗漏的以后发现再补上.接下来的章节主要都是讲解在本框架的基础上进行开发的小巧. 本框架主要有四 ...

  7. c 进程和系统调用

    这一篇博客讲解进程和系统调用相关的知识 有这样一个场景,我需要输入一串文字,然后把我输入的文字加上一个本地的时间戳 保存在一个文件中,可以初步理解为一个备忘录也行 #include <stdio ...

  8. objC与js通信实现--WebViewJavascriptBridge

    场景   在移动端开发中,最为流行的开发模式就是hybmid开发,在这种native和h5的杂糅下,既能在某些需求中保证足够的性能,也可以在某些列表详情的需求下采用h5的样式控制来丰富内容.但是在大型 ...

  9. 密码学应用(DES,AES, MD5, SHA1, RSA, Salt, Pkcs8)

    目录 一.数据加密标准 - Data Encryption Standard(DES) 二.高级加密标准 - Advanced Encryption Standard(AES) 三.消息摘要算法第五版 ...

  10. ASP.NET Core 整合Autofac和Castle实现自动AOP拦截

    前言: 除了ASP.NETCore自带的IOC容器外,我们还可以使用其他成熟的DI框架,如Autofac,StructureMap等(笔者只用过Unity,Ninject和Castle). 1.ASP ...