python 字串格式化 password密文

2021-10-01 12:59:59 字數 3754 閱讀 3714

tab 補全

for linux

vim tab.py

#!/usr/bin/env python

# python startup file

import sys

import readline

import rlcompleter

import atexit

import os

# tab completion

readline.parse_and_bind('tab: complete')

# history file

histfile = os.path.join(os.environ['home'], '.pythonhistory')

try:

readline.read_history_file(histfile)

except ioerror:

pass

atexit.register(readline.write_history_file, histfile)

del os, histfile, readline, rlcompleter

寫完儲存後就可以使用了

localhost:~ jieli$ python

python 2.7.10 (default, oct 23 2015, 18:05:06)

>>> import tab

你會發現,上面自己寫的tab.py模組只能在當前目錄下匯入,如果想在系統的何何乙個地方都使用怎麼辦呢? 此時你就要把這個tab.py放到python全域性環境變數目錄裡啦,基本一般都放在乙個叫 python/2.7/site-packages 目錄下,這個目錄在不同的os裡放的位置不一樣,用 print(sys.path) 可以檢視python環境變數列表。

變數設定規範

變數包括

數字字母

變數不能以數字開頭

ps:數字不能開頭

不能是關鍵字

最好不要和python 內建的東西重複

條件語句

if 條件1 :

pass

elif 條件2:

else:

while 迴圈

continue 終止當前迴圈,開始下一次迴圈

break 終止所有迴圈

_*_ coding:utf8 _*_

數字python2

int 整

long 長整型

python3

整型字串

s1='bacb'

a='123'

b=int(a)

10進製16進製制轉換

v=int(num,base=16)

字元居中

s='abcde'

s.center(20)

s.center(20,'*')

s.count('ab')

test='i am , age'

test.format('pang',19)

test.format(name='pang',age=19)

test.format_map()

test.index('8')

字串中是否只包含數字或字母

name='alex'

print(name.strip())

print(name.startswith('al'))

print(name.endswith('x'))

print(name.replace('l','p'))

print(name.split('l'))

print(name.upper())

print(name.lower())

print(name[1])

print(name[0:3])

print(name[-3:-1])

print(name.getitem('e'))

keywords=input('please input searech you connect:')

if "蒼老師" in keywords:

print(keywords.replace('蒼老師','*********'))

elif "***" in keywords:

print(keywords.replace('***','****'))

else:

print('you search connect is %s' % keywords)

python 生成隨機數、隨機字串

#!/usr/bin/python # -*- coding: utf-8 -*- import random import string # 隨機整數: print random.randint(1,50) # 隨機選取0到100間的偶數: print random.randrange(0, 101, 2) # 隨機浮點數: print random.random() print random.uniform(1, 10) # 隨機字元: print random.choice('abcdefghijklmnopqrstuvwxyz!@#$%^&*()') # 多個字元中生成指定數量的隨機字元: print random.sample('zyxwvutsrqponmlkjihgfedcba',5) # 從a-za-z0-9生成指定數量的隨機字元: ran_str = ''.join(random.sample(string.ascii_letters + string.digits, 8)) print ran_str # 多個字元中選取指定數量的字元組成新字串: print ''.join(random.sample(['z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'], 5)) # 隨機選取字串: print random.choice(['剪刀', '石頭', '布']) # 打亂排序 items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] print random.shuffle(items)

字串格式化

user_info

name = input('name:')

age = int(input('age:'))

job = input('job:')

salary=input('salary:')

user_info='''

-------------- info of %s -------------------

name: %s

age: %d

job: %s

salary: %s

'''% (name,name,age,job,salary)

info2='''

-------------- info of -------------------

name:

age:

job:

salary:

'''.format(_name=name,_age=age,_job=job,_salary=salary)

info2='''

-------------- info of -------------------

name:

age:

job:

salary:

'''.format(_name,_age,_job,_salary)

密文輸入

import getpass

password=getpass.getpass("password:")

Python 字串格式化

字串格式化 s 格式化為字串 format hello,s.s enough for ya?values world hot print format values hello,world.hot enough for ya?f 格式化為實數 浮點數 format pi with three dec...

python字串格式化

字串的格式化 在python中也有類似於c中的printf 的格式輸出標記。在python中格式化輸出字串使用的是 運算子,通用的形式為 其中,左邊部分的 格式標記字串 可以完全和c中的一致。右邊的 值組 如果有兩個及以上的值則需要用小括號括起來,中間用短號隔開。重點來看左邊的部分。左邊部分的最簡單...

Python字串格式化

字串的格式化 在python中也有類似於c中的printf 的格式輸出標記。在python中格式化輸出字串使用的是 運算子,通用的形式為 其中,左邊部分的 格式標記字串 可以完全和c中的一致。右邊的 值組 如果有兩個及以上的值則需要用小括號括起來,中間用短號隔開。重點來看左邊的部分。左邊部分的最簡單...