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

>>> 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. WCF学习之旅—HTTP双工模式(二十)

    WCF学习之旅—请求与答复模式和单向模式(十九) 四.HTTP双工模式 双工模式建立在上文所实现的两种模式的基础之上,实现客户端与服务端相互调用:前面介绍的两种方法只是在客户端调用服务端的方法,然后服 ...

  2. 【原】SDWebImage源码阅读(四)

    [原]SDWebImage源码阅读(四) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 SDWebImage中主要实现了NSURLConnectionDataDelega ...

  3. 【原】使用Bmob作为iOS后台开发心得——云端代码添加其他User的Relation关系

    本文转载请注明出处 —— polobymulberry-博客园 问题描述 我在User表中增加了两个列,分别为“我关注的人”(Relation关系)和“我的粉丝”(Relation关系)当我关注某个人 ...

  4. 【译】Unity3D Shader 新手教程(2/6) —— 积雪Shader

    本文为翻译,附上原文链接. 转载请注明出处--polobymulberry-博客园. 如果你是一个shader编程的新手,并且你想学到下面这些酷炫的技术,我觉得你可以看看这篇教程: 实现一个积雪效果的 ...

  5. Java学习之LinkedHashMap学习总结

    前言: 在学习LRU算法的时候,看到LruCache源码实现是基于LinkedHashMap,今天学习一下LinkedHashMap的好处以及如何实现lru缓存机制的. 需求背景: LRU这个算法就是 ...

  6. 如何修复VUM在客户端启用之后报数据库连接失败的问题

    在上一篇随笔中介绍了关于重新注册VMware Update Manager(VUM)至vCenter Server中的方法,最近有朋友反应,原本切换过去好好的更新服务为什么某次使用一下就不灵了? 当时 ...

  7. 安卓第一次启动引导页使用ViewPager实现

    我们在安装某个APP的时候,基本都会有一个引导页的提示,他们可以打广告,或者介绍新功能的加入和使用说明等.一般都支持滑动并且下面有几个点,显示共有多少页和当前图片的位置,在IOS上这个实现起来比较简单 ...

  8. Entity Framework入门系列(1)-扯淡开篇

    这是我在Cnblogs上的第一个系列,但愿能坚持下去: 惯例索引 Entity Framework入门系列(1)-开篇兼索引: Entity Framework入门系列(2)-初试Code First ...

  9. 3.EF 6.0 Code-First实现增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-entity-framework-5-0-code- ...

  10. asp.net MVC4——省市三级联动

    controller: public ActionResult GetCity(string id) { AreaService _areaSvc = new AreaService(); List& ...