Search code examples
pythonraspberry-piespeak

How to play a dynamic string with espeak in python


I am using the espeak library for text to speech conversion.I am able to generate a dynmic sound from a string to do this.

os.system('espeak "hello"')

This works. But what i need is to generate sound from a string. This is what i did

string='hello'
os.system('espeak string')

Solution

  • Just interpolate the string that you want to speak into your command.

    >>> string = "Hello"
    >>> os.system('espeak "{}"'.format(string))
    

    You could use the subprocess.Popen function if you are accepting user input for the string.

    >>> import subprocess
    >>> p = subprocess.Popen(['espeak', string])