1. 程式人生 > >pycharm中python程式碼格式化方法

pycharm中python程式碼格式化方法

在使用pycharm進行python編碼時,經常會遇到程式碼中少了很多空格,或者是縮排不標準的情況,例如等號左右沒有空格,陣列中逗號後面沒有空格等等。

Ctrl+A全選後使用快捷鍵 Ctrl+Alt+L 可以批量格式化程式碼。

例如:程式碼格式化前:

import random
# 生成隨機浮點數
float1=random.uniform(10,20)
float2=random.uniform(1,3)
# 生成隨機整數[a,b]之間,閉區間
int1=random.randint(10,20)
int2=random.randint(1,3)
# 隨機字元
str1=random.choice('abcdefg')
# 隨機字串
str3 = random.choice(['apple','pear','peach','orange','lemon'])
# 隨機打亂#洗牌
items=[1,2,3,4,5,6]
random.shuffle(items)

格式化後:

import random

# 生成隨機浮點數
float1 = random.uniform(10, 20)
float2 = random.uniform(1, 3)
# 生成隨機整數[a,b]之間,閉區間
int1 = random.randint(10, 20)
int2 = random.randint(1, 3)
# 隨機字元
str1 = random.choice('abcdefg')
# 隨機字串
str3 = random.choice(['apple', 'pear', 'peach', 'orange', 'lemon'])
# 隨機打亂#洗牌
items = [1, 2, 3, 4, 5, 6]
random.shuffle(items)