python常用模組介紹之一:string模組
簡介:
string模組主要用於對字串進行操作。string裡的許多特性都已經移植到str和unicode物件的方法裡去了。下面主要討論下string模組的常用方法。
- 函式
1. string.atof(s) 字串轉換成浮點型
string.atof('1.11')
輸出結果:1.11
string.atof('1')
輸出結果:1.0
2. string.atoi(s[, base]) 字串轉換成整型
string.atoi('11') or string.atoi('11', 10)
輸出結果:11
string.atoi('11', 2)
輸出結果:3
string.atoi('11', 8)
輸出結果:9
string.atoi('11', 16)
輸出結果:17
3. string.capitalize(s) 字串的第一個字元轉換成大寫
string.capitalize('hello world')
輸出結果:Hello world
4. string.capwords(s[, sep]) 字串以sep為分隔符分割後的每個欄位的首位轉換為大寫
string.capwords('hello world')
輸出結果:Hello World
string.capwords('hello world', 'l')
輸出結果:HellO worlD
5. string.center(s, len[, fillchar])字串轉換成指定長度,不夠的用fillchar補充,且補充的字元在兩邊
string.center('hello world', 10, '*')
輸出結果:hello world
string.center('hello world', 15, '*')
輸出結果:**hello world**
6. string.count(s, sub[, start[, end]])查詢sub在s中的個數
string.count('hello world', 'l')
輸出結果:3
string.count('hello world', 'l', 3)
輸出結果:2
string.count('hello world', 'l', 3, 6)
輸出結果:1
7. string.find(s, sub[, start,[end]]) 查詢sub在s中的第一個位置
string.find('hello world', 'l')
輸出結果:2
string.find('hello world', 'l', 4, 6)
輸出結果:-1
8. string.ljust(s, len[, fillchar])字串左對齊,不夠用fillchar補充
string.ljust('hello world', 15)
輸出結果:hello world
string.ljust('hello world', 15, '*')
輸出結果:hello world****
9. string.lstrip(s[, chars]) 清除左邊的空白字元
string.lstrip(' hello world')
輸出結果:hello world
string.lstrip('hello world', 'h')
輸出結果:ello world
10. string.upper(s) 字串轉換成大寫的
string.upper('hello world')
輸出結果:HELLO WORLD
11. string.join(list[, sep]) list裡的字串用sep連線起來
string.join(['hello', 'world'])
輸出結果:hello world
string.join(['hello', 'world'], '*')
輸出結果:hello*world
12. string.replace(s, old, new[,max]) 字串s裡的old替換為new,最多替換為max次
輸出結果:heLLo worLd
string.replace('hello world', 'l', 'L', 1)
輸出結果:heLlo world
13. string.translate(s, table[,delchar]) 字串轉換為指定的table裡的字元,且刪除指定的delchar
table = string.maketrans('hello', 'HELLO')
string.translate('hello world', table)
輸出結果:HELLO wOrLd
string.translate('hello world', table, 'l')
輸出結果:HEO wOrd
14. string.split(s[, sep[,maxsplit]]) 字串以sep作為分隔符,maxsplit作為分隔次數進行分隔
string.split('hello world')
輸出結果:['hello', 'world']
string.split('hello world', 'l')
輸出結果:['he', '', 'o wor', 'd']
string.split('hello world', 'l', 1)
輸出結果:['he', 'lo world']
- 模板
map = {'var': 'hello world'}
tmp = string.Template('my first output:${var}')
tmp.substitute(map)
輸出結果:my first output: hello world
tmp.safe_substitute(map)
輸出結果:同上
map = {'var': 'hello world'}
tmp = string.Template('my first output:${vars}')
tmp.substitute(map)
輸出結果:
tmp.safe_substitute(map)
輸出結果:my first output: ${vars}
相關推薦
python常用模組介紹之一:string模組
簡介: string模組主要用於對字串進行操作。string裡的許多特性都已經移植到str和unicode物件的方法裡去了。下面主要討論下string模組的常用方法。 函式 1. string.atof(s) 字串轉換成浮點型 string.a
Python 資料處理擴充套件包: pandas 模組的DataFrame介紹(建立和基本操作)
DataFrame是Pandas中的一個表結構的資料結構,包括三部分資訊,表頭(列的名稱),表的內容(二維矩陣),索引(每行一個唯一的標記)。 一、DataFrame的建立 有多種方式可以建立DataFrame,下面舉例介紹。 例1: 通過list建立 >
python之正則表示式:re模組
一.正則表示式中常用的字元含義 1、普通字元和11個元字元: 常用字元劃分 匹配範圍 示例資料 匹配的正則表示式 目標匹配的字串 普通字元 匹配自身 abc
Python常用庫urllib中urllib.request模組使用詳解
1.urllib2和urllib庫的區別 Urllib庫是Python中的一個功能強大、用於操作URL,並在做爬蟲的時候經常要用到的庫。在Python2.x中,分為Urllib庫和Urllin2庫,P
python訪問excel基本用法:openpyxl模組(一)
準備一個excel檔案,這裡是:e\t.xlsx,放檔案裡隨便寫幾條資料。 注意:副檔名必須是xlsx(excel 2007及以上版本的),因為openpyxl模組只能處理xlsx檔案。 一、安裝op
Python 爬蟲學習筆記二: xpath 模組
Python 爬蟲學習筆記二: xpath from lxml 首先應該知道的是xpath 只是一個元素選擇器, 在python 的另外一個庫lxml 中, 想要使用xpath 必須首先下載lxml 庫 lxml 庫的安裝: 很簡單, 具體請檢視 http:
Python分散式程序報錯:pickle模組不能序列化lambda函式
今天在學習到廖老師Python教程的分散式程序時,遇到了一個錯誤:_pickle.PicklingError: Can't pickle <function <lambda> at 0x000001710FDC2EA0>: attribute lo
Python 實現FTP客戶端:ftplib模組的使用
1. FTP常用方法總結如下: from ftplib import FTP 匯入包 ftp = FTP() 例項化ftp物件 ftp.set_debuglevel(2) 開啟除錯級別2,顯示詳細資訊 ftp.set_debuglevel(0)
三、【笨辦法學python】學習隨筆之一:格式化字元
python中的格式化字元 語法 我們舉個例子來說明格式化字串的語法 \>>> 'Hello, %s' % 'world' 'Hello, world' \>>> 'Hi, %s, you have $%d.' % ('Michael
【C++】STL常用容器總結之一:容器與迭代器
宣告: 1、本博文主要整理自《C++ Primer》和《STL原始碼剖析》這兩本經典書籍。同時,也參考了網路中不少優秀部落格,對這些部落格的作者表示感謝。 2、由於博主能力有限,對於一些容器的用法可能尚未進行深入研究。因此,本博文若有錯誤和不足之處,歡迎大家
python常用模組介紹之三:logging模組
簡介: Python的logging模組提供了通用的日誌系統,可以方便第三方模組或者是應用使用。這個模組提供不同的日誌級別,並可以採用不同的方式記錄日誌,比如檔案,HTTP GET/POST,SMTP,Socket等,甚至可以自己實現具體的日誌記錄方式。 模組
Learning-Python【21】:Python常用模組(4)—— re、logging、hashlib、subprocess
re 模組:與正則相關的模組 在使用 re 模組之前,需要先了解正則表示式(regular expression),描述了一種字串匹配的模式(pattern),可以用來檢查一個字串是否含有某個子字串、將匹配的子字串替換或者從某個字串中取出符合某個條件的子字串等。 import
Learning-Python【19】:Python常用模組(2)—— os、sys、shutil
os模組:與作業系統相關的模組 import os # 獲取當前的工作目錄 print(os.getcwd()) # 切換工作目錄 os.chdir(r'E:\Python\test') print(os.getcwd()) # 當前目錄, 一個點 print(o
Python:os 模組常用方法簡介
os.getcwd()# 返回當前工作目錄 os.path.abspath(path)# 返回 path 的絕對路徑# os.path.abspath('.') 相當於 os.getcwd() os.path.split(path)# 返回 tuple(頭部, 尾部),尾部是最終斜線後的所有內容# 一般用
Python學習【第8篇】:Python之常用模組一(主要是正則以及collections模組) python--------------常用模組之正則
python--------------常用模組之正則 一、認識模組 什麼是模組:一個模組就是一個包含了python定義和宣告的檔案,檔名就是加上.py的字尾,但其實import載入的模組分為四個通用類別 :
每日一python(5):json模組中的相關方法介紹
1、json.dumps()和json.loads()函式 說明: json.dumps()函式是將一個Python資料型別進行json格式的編碼(簡單理解,json.dumps()函式是將字典轉化為字串) json.loads()函式是將json格式資料轉換為字典
python:pandas模組中的DataFrame結構及常用操作
轉載:http://blog.csdn.net/u014607457/article/details/51290582 1. 介紹 DataFrame unifies two or more Series into a single data structure.Ea
python的functools模組常用方法介紹
functools模組中有兩個方法是從_functools引入的,分別是partial和reduce函式。 functools模組是python2.5版本新增的模組。 1.偏函式partial python中的偏函式與數學的偏函式意義不同,python中的
python常用內建模組:datetime
使用 匯入datetime模組下的datetime類,如果只是import datetime 那麼呼叫要通過datetime.datetime 舉例:返回當前日期和時間: >>> from datetime import datetime
Python全棧修煉日記之一:編程語言的介紹
應用 應用程序 提示符 family 編譯 tint 初學者 程序 microsoft Python 簡介 Python 是一個高層次的結合了解釋性、編譯性、互動性和面向對象的腳本語言。 Python 的設計具有很強的可讀性,相比其他語言經常使用英文關鍵字,其他語言的一些