Back to snippets

pypinyin_chinese_to_pinyin_conversion_with_tone_styles.py

python

Basic usage of pypinyin to convert Chinese characters into pinyin with differen

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)
4result1 = pinyin('中心')
5print(result1)
6# Output: [['zhōng'], ['xīn']]
7
8# Specify pinyin style (e.g., Tone 2 style: zho1ng xi1n)
9result2 = pinyin('中心', style=Style.TONE2)
10print(result2)
11# Output: [['zho1ng'], ['xi1n']]
12
13# Lazy pinyin (returns a simple list of strings without tone marks)
14result3 = lazy_pinyin('中心')
15print(result3)
16# Output: ['zhong', 'xin']