Source: http://www.liaoxuefeng.com/

❤ Escape character: '\'

- '\n': newline;

- '\t': tab;

- '\\': \;

- r'...': no transferring for contents within single quotes;

- '''...''': multiple lines within triple quotes: could start a new line with ENTER directly. r'''...''' is valid as well.

❤ Division

- '/': floating point calculation, alway return float;

- '//': only return integer part (decimal part is direclty abandoned);

- '%': return remainder.

❤ Character encoding

- Encoding standard

.. ASCII: some symbold, number, uppercase and lowercase, 1 byte representing 1 character;

.. GB2312: Chinese character;

.. Unicode: encoding characters in all languages using one criterion, 2 bytes representing 1 character usually;

.. UTF-8: to save space, converting Unicode into 1-6 bytes (usually, one English character: 1 bytes, while one Chinses: 3 bytes); criterion used in RAM;

- Python

.. encoding using unicode;

.. for sigle character, ord() - obtaining integral representing for the character; chr() - converting code into character;

.. b'...': converting string to byte format (one byte fot each character);

.. '...'.encode('method'): encode ... using corresponding method('ascii', 'utf-8', note that Chinese characters cannot be encoded using 'ascii');

.. b'...'.decode('method'): decode byte string '...' into characters using corredponding method ('ascii', 'utf-8');

.. len('...'): obtain number of characters or bytes in '...';

.. formatting characters: the same way as c (and matlab :p):

%s - character string;

%d: integer;

%f: floating;

%x: hexadecimal integer.

some examples:


>>> 'Hello, %s' % 'world'
'Hello, world'
>>> 'Hi, %s, you have $%d.' % ('Michael', 1000000)
'Hi, Michael, you have $1000000'.
>>> '%2d-%02d' % (3, 1)
'3-01'
>>> '%.2f' % 3.1415
'3.14'
>>> 'Age: %s; Gender: %s' % (25, True)   # If you cannot decide which placeholder to use, just use %s
'Age: 25; Gender: true'
>>> 'Growth rate: %d %%' % 7
'Growth rate: 7 %'

NOTE: we should stick to 'utf-8' for converting to avoid chaos, to ensure which: 1. make sure your text editor is using: "UTF-8 without BOM" as encoding methods; 2. add the following two lines at the beginning of your script.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

Meet Python: little notes的更多相关文章

  1. Meet Python: little notes 3 - function

    Source: http://www.liaoxuefeng.com/ ♥ Function In python, name of a function could be assigned to a ...

  2. Meet Python: little notes 2

    From this blog I will turn to Markdown for original writing. Source: http://www.liaoxuefeng.com/ ♥ l ...

  3. Meet python: little notes 4 - high-level characteristics

    Source: http://www.liaoxuefeng.com/ ♥ Slice Obtaining elements within required range from list or tu ...

  4. python 100day notes(2)

    python 100day notes(2) str str2 = 'abc123456' print(str1.endswith('!')) # True # 将字符串以指定的宽度居中并在两侧填充指 ...

  5. 70个注意的Python小Notes

    Python读书笔记:70个注意的小Notes 作者:白宁超 2018年7月9日10:58:18 摘要:在阅读python相关书籍中,对其进行简单的笔记纪要.旨在注意一些细节问题,在今后项目中灵活运用 ...

  6. [Python Study Notes]匿名函数

    Python 使用 lambda 来创建匿名函数. lambda这个名称来自于LISP,而LISP则是从lambda calculus(一种符号逻辑形式)取这个名称的.在Python中,lambda作 ...

  7. [Python Study Notes]字符串处理技巧(持续更新)

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  8. [Python Study Notes]with的使用

    在 Python 2.5 中, with 关键字被加入.它将常用的 try ... except ... finally ... 模式很方便的被复用.看一个最经典的例子: with open('fil ...

  9. [Python Study Notes]实现对键盘控制与监控

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

随机推荐

  1. c中的数组与字符串

    一. 数组的基本概念 一个int类型的变量能保存一个人的年龄,如果想保存整个班的年龄呢? 1. 什么是数组 数组,从字面上看,就是一组数据的意思,没错,数组就是用来存储一组数据的 2. 数组的特点 l ...

  2. iOS 杂笔-22(万年一遇~一张图片对代理的理解)

    iOS 杂笔-22(万年一遇~一张图片对代理的理解) 建议:本博客需要对代理有一定了解方可阅读(反正我也管不到) 图片 在图片之外设置协议(没有这东西这篇博客也就是夭折了) 下面我对图片中出现的形形色 ...

  3. SVProgressHUD进度条蒙版

    GitHub:https://github.com/samvermette/SVProgressHUDSVProgressHUD和MBProgressHUD效果差不多,不过不需要使用协议,同时也不需要 ...

  4. #VSTS 日志# VSTS 所有功能,看这个页面就够了!

    随着Connect();//2015大会的结束,一大波的好消息随之而来.今天小编刚刚发现了Visual Studio Team Services / Team Foundation Server 的完 ...

  5. Hbase安装配置(靠谱亲测)

    Hbase是Hadoop生态系统中的NoSql列式数据库.通过Hbase,可以进行数据读写,比较适合Top n场景.Hbase搭建的系统,瓶颈在于硬盘的传输速度.RDBMS一般的瓶颈在于寻道速度. 实 ...

  6. [css]我要用css画幅画(一)

    几年前开始就一直想用css画幅画. 今天才真正开始, 从简单的开始. 作为一个工作压力那么大的程序员,我首先要画一个太阳. html如下: <!DOCTYPE html> <html ...

  7. yii2增加验证码详细步骤

    作者:白狼 出处:http://www.manks.top/article/yii2_captcha本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留 ...

  8. 更换mysql数据目录后出现ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 的解决办法

    服务器上的mysql默认数据目录为/var/lib/mysql/,同时服务器的/空间不是很大,而近期又有大量的日志需要导入进行分析,时常搞得/的空间捉襟见肘,晚上一狠心就想把mysql的数据目录转移到 ...

  9. [MySQL Reference Manual] 4 MYSQL Program

    4 MYSQL Program 目录 4 MYSQL Program 4.3 MySQL Server和Server启动程序 4.3.1 mysqld 4.3.2 mysqld_safe 4.3.3 ...

  10. Innodb行锁源码学习(一)

    Innodb是mysql数据库中目前最流行的存储引擎,innodb相对其它存储引擎一个很大的特点是支持事务,并且支持行粒度的锁.今天我重点跟大家分享下innodb行锁实现的基础知识.由于篇幅比较大,文 ...