python logo

Category: uncategorized

save dictionary python

python string methods

python find in array

python pause for time

Speech engines with python tutorial

Text To Speech (TTS) Text To Speech (TTS)

A computer system used to create artificial speech is called a speech synthesizer, and can be implemented in software or hardware products.

A text-to-speech (TTS) system converts normal language text into speech. How can we use speech synthesis in Python?

Related courses:


Pyttsx is a cross-platform speech (Mac OSX, Windows, and Linux) library. You can set voice metadata such as age, gender, id, language and name. Thee speech engine comes with a large amount of voices.

Text to speech sample:

Install with:

sudo pip install pyttsx

Create the code

import pyttsx
engine = pyttsx.init()
engine.say('The quick brown fox jumped over the lazy dog.')

And execute it with python.


eSpeak is a compact open source software speech synthesizer for English and other languages, for Linux and Windows.

Text to speech sample:

We can install using:

sudo apt-get install espeak

Create the code

import os
os.system("espeak 'The quick brown fox'")

It is very easy to use, but like pyttsx it sounds very robotic.


The gtts module no longer works.

I found a script on Github that uses the Google speech engine. The script comes with many options and does not speak, instead it saves to an mp3. We added a command to play the mp3 automatically:

os.system("mpg321 out.mp3 -quiet")

Run with:

python -s 'Python programming example'

The voice is extremely natural. The only disadvantage is that you need to be connected with the Internet when running this script.


You might like:

string methods python

Methods can be applied to Python strings, where a string is somet text between quotes. An example of using the string method replace, which replaces the a sub-string in a string:

s = "Hello World"
s = s.replace("World","Python")

Related Course:
Python Programming Bootcamp: Go from zero to hero

Python includes these useful methods for string manipulation:

python math operators

Overview of Python IDEs and Code Editors

Install a Python IDE

An Integrated Desktop Environment (IDE) is a software application for programming. In addition to simple text editing they have all kind of features such as syntax highlighting, code completion, tabs, a class browser and many more.

Online Python Interpreters

The online interpreters may not work for everything but will work for most of the beginner tutorials. I recommend using a desktop IDE or the official Python interpreter.

Overview of IDEs (You only need one)

Method with description
Returns first letter capitalized string.
Count the frequency of the word str. It can also be applied to words: s.count("Hello")
Find the position of the first occurence.
Find the position of the first occurence, raise exception if not found.
Returns true if string is decimal.
Returns true if string contains digits only.
Returns true if string contains numeric only.
Returns string in lowercase.
Returns the length of str.
Returns string in uppercase.
replace(old, new)
Replaces the first argument with the second argument.

jarvis python code

I thought it would be cool to create a personal assistant in Python. If you are into movies you may have heard of Jarvis, an A.I. based character in the Iron Man films. In this tutorial we will create a robot.

In this tutorial you get started with coding your own Jarvis, the voice activated assistant in Iron Man. Jarvis is a voice assistant, similar to Apple’s Siri or Google Now. In this tutorial we use the power of the Python programming language and a text-to-speech service.

I’m going to be using Ubuntu Linux for this project but you should be able to use it in Windows or Mac as well. However, since there’s quite a bit of command line work required, I’d recommend doing this on a Linux machine.

The features I want to have are:

For this tutorial you will need (Ubuntu) Linux, Python and a working microphone.

Related course:


This is what you’ll create (watch the whole video, demo at the end):

Recognize spoken voice

Speech recognition can by done using the Python SpeechRecognition module. We make use of the Google Speech API because of it’s great quality.

Answer in spoken voice (Text To Speech)

Various APIs and programs are available for text to speech applications. Espeak and pyttsx work out of the box but sound very robotic. We decided to go with the Google Text To Speech API, gTTS.

IDE Author Platform Description Price Download
PyCharm Jetbrains Windows, Mac OS X, Linux/UNIX Python IDE. Features including: code completion, code inspections, on-the-fly error highlighting and quick-fixes € 89 / 1st year.($ 97.90) Download PyCharm 
Atom (+script plugin) GitHub Windows, Mac OS X, Linux/UNIX Python IDE. You need to download the script plugin after installing Atom. Free. Download Atom.
Pythonista omz:software Apple iOS (iPhone, iPad) Features include: Syntax highlighting, code completion, interactive prompt, standard and iOS modules. € 9. ($ 9.90) Download Pythonista.
Eclipse with PyDev Aleks Totic Windows, Mac OS X, Linux/UNIX Features include: Syntax highlighting, code refactoring, graphical debugging and more. Free Download 
Eric Python IDE Detlev Offenbach Windows, Linux/UNIX Features include: Syntax highlighting, autocompletion, class browser and more. Free Download 
Wing IDE Wingware Windows, Mac OS X, Linux/UNIX Features: Syntax highlighting, auto completion, refactoring, unit testing and version control. $45 to $245 per user / license. Download 
Komodo IDE Komodo Windows, Mac OS X, Linux/UNIX Features: Syntax highlighting, documentation browser, run code in-line, quick bookmarks and more. € 40 to € 223. ($99 to $295). Download Amjad Masad, Haya Odeh, Faris Masad and Max Shawabkeh. Web Python interpreter Free Run Online 
Ideone Ideone Web Python interpreter Free Run Online 
Codepad Steven Hazel Web Python interpreter Free Run Online 

sudo pip install gTTS

Using it is as simple as:

from gtts import gTTS
import os
tts = gTTS(text='Hello World', lang='en')"hello.mp3")
os.system("mpg321 hello.mp3")


Complete program

The program below will answer spoken questions.

#!/usr/bin/env python3
# Requires PyAudio and PySpeech.

import speech_recognition as sr
from time import ctime
import time
import os
from gtts import gTTS

def speak(audioString):
tts = gTTS(text=audioString, lang='en')"audio.mp3")
os.system("mpg321 audio.mp3")

def recordAudio():
# Record Audio
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)

# Speech recognition using Google Speech Recognition
data = ""
# Uses the default API key
# To use another API key: `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
data = r.recognize_google(audio)
print("You said: " + data)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))

return data

def jarvis(data):
if "how are you" in data:
speak("I am fine")

if "what time is it" in data:

if "where is" in data:
data = data.split(" ")
location = data[2]
speak("Hold on Frank, I will show you where " + location + " is.")
os.system("chromium-browser" + location + "/&")

# initialization
speak("Hi Frank, what can I do for you?")
while 1:
data = recordAudio()

Related posts:


gui in python

To create a graphical interface (GUI) in Python you need to make use of a library or module.  There are at least three widely used modules for creating GUIs with Python:

While you can create a graphical user interface (GUI) with any of these modules, they do not have the same features.

Related course:

GUI toolkits

Tk provides basic widgets such as a button, menu, text and label. It is very limited compared to QT4 and WxPython but it is also the oldest module.  It runs on most versions of Mac OS, Linux and Windows.

TkMessage box a messagebox in Tk. GUIs made in Tk do not have a native look.

QT4 and QT5 are developed by the Qt company.  Graphical applications made with QT4 or QT5 run on Windows, Linux/X11 and Mac OS X.  It has tons of widgets including tabs, buttons, item views, table views, progressbars, input fields, calendar views and many more. Compared to Tk it has a lot more widgets available.

Graphical application using PyQt5

WxPython is a module that creates a native GUI look regardless of the operating system used. On Windows it will look as windows application while on Mac OS it will look as a Mac application. This can be a clear advantage compared to QT4 or Tk, depending on your purpose. WxWidgets has many widgets available such as buttons, menu, text but also more advanced widgets as a htmlview or tree control.

wxTabs A window created with wxPython. wxPython has native appearance on all operating systems.