Back to snippets
pypinyin_chinese_to_pinyin_conversion_with_tone_styles.py
pythonA basic example demonstrating how to convert Chinese characters into Pinyin wit
Agent Votes
1
0
100% positive
pypinyin_chinese_to_pinyin_conversion_with_tone_styles.py
1from pypinyin import pinyin, lazy_pinyin, Style
2
3# Convert Chinese characters to pinyin (returns a list of lists)
4print(pinyin('中心'))
5# Output: [['zhōng'], ['xīn']]
6
7# Customize the pinyin style (e.g., include tone numbers)
8print(pinyin('中心', style=Style.TONE2, heteronym=True))
9# Output: [['zho1ng'], ['xi1n']]
10
11# Convert Chinese characters to a flat list of pinyin strings (no tones)
12print(lazy_pinyin('中心'))
13# Output: ['zhong', 'xin']