The question

I'd like to have a function in Python for splitting a given string containing pinyin into its syllables. The following two code blocks show how the function is intended to work.


huǎngrán dà wù


tàng suǒ
fēng tǔ rén qíng
dé bù cháng shī
huǎng rán dà wù

Additional notes

The function should support pinyin of all Chinese characters. I emphasize this because I have noticed that some software that input pinyin can't type the syllables shown in the code block below.




hū lüè

For the record, I'm considering the pinyin that is taught in the HSK Standard Course book series (i.e. pinyin of Standard Mandarin).

  • 2
    I don't know enough code to help on that side. However your biggest issue is not a code one, but ambiguous pinyin. for example mìngā could be mìng ā or mìn gā-- huànshi could be huàn shi or hu àn shi (usually written hu'àn for clarity but not always). also basic auto pinyin issues such as wrong tone or pronunciation in the source should be considered.
    – zagrycha
    Feb 27, 2023 at 4:10
  • I agree largely with what zagrycha said, this is a coding issue and can't be easily done. You must consider the frequency of which each word is used, and the nasal tones can be confusing to your program. You can consider using a dictionary (tedious, and sadly doesn't distinguish nasal tones, but maybe solvable with an if statement because nasal with ng usually doesn't follow with another vowel). BUT largely this is not a Chinese problem but a coding problem
    – Wolfking
    Feb 27, 2023 at 5:28
  • I think you'd do okay by breaking after non-extensible pinyin finals (and just give a warning for the ambiguous cases). Nevertheless, there's a plethora of boundary cases like capital letters (e.g. 俄罗斯 in pinyin), punctuation, numbers, English words, errors in the input, and peculiar characters like and .
    – Becky 李蓓
    Feb 27, 2023 at 6:22
  • According to Pinyin Rules, an apostrophe (隔音符号,单引号)must be inserted before a vowel (a. e. o) in case it starts a new syllable in the middel of a word, such as in (xi'an). In the word tian'anmen, you still need the (') symbol, even if there is no word tia-nan-men. (no the sound of tia).
    – PdotWang
    Feb 27, 2023 at 20:14
  • 1
    Commenting rather than answering because this isn't in Python, but this project in TypeScript seems to be decently well-tested, and uses a list of common syllables. I agree that ambiguity would make this brittle, but maybe converting this to Python, possibly adding more common syllables, and sorting them by commonality, would get you pretty close.
    – Zac Anger
    Mar 4, 2023 at 9:13

1 Answer 1


The objective of the project: Splitting a given string containing pinyin into its syllables.

I would do it with the following steps. There could be exceptions, therefore testing is very important.

(1) Separate all characters.

(2) Combine adjacent vowels (ai, ei, ao, ou, ia, ie, iu, ua, uo, ui, ve, iao). *1

(3) combine adjacent consonants (ng, ch, sh, zh). *2

(4) combine finals with n and ng.

(5) combine initials and finals to form the syllables. *3

*1 I am not quite sure about er in (二)and (一会儿).

*2 combine n and g only if g is followed by a consonant or a separate symbol. The official separate symbol is ('), such as in (xi'an).

*3 Some syllables do not have an initial but only a final.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.