2018年3月29日 星期四

[ Python 常見問題 ] How do I iterate through the alphabet in Python?

Source From Here 
Question 
As title. How do I iterate a-z without creating my own list of chars? 

How-To 
You can use string.ascii_lowercase which is simply a convenience string of lowercase letters: 
>>> from string import ascii_lowercase 
>>> ascii_lowercase 
'abcdefghijklmnopqrstuvwxyz' 
>>> for c in ascii_lowercase: 
... print('Char {}'.format(c)) 
... 
Char a 
Char b 
... 
Char z


沒有留言:

張貼留言

[ Py DS ] Ch2 - Introduction to NumPy (Part1)

Source From  Here   Preface   This chapter, along with Chapter 3, outlines techniques for effectively loading, storing, and manipulating i...