Heyy coder!! Welcome to another amazing lesson from Noob Code Pro! Today, we will be creating a Python program that will actually speak to you. All you need to do in order to do this is, connect to your nearest WiFi and follow the steps explained in this article. Without wasting any more of your coding time, let’s jump right into it.

INSTALLING PYTTSX3:

‘pyttsx3’ is a python library that has got some cool functions which will help us interact with our computer verbally. Basically, this library will help us command our computer with speech and also enable the computer to speak to us. 

It is a very useful library and can be used to create programs like a virtual assistant.

In order to install pyttsx3, all you have to do is open Command Prompt as an administrator,

and then execute the following command in the Command Prompt:

python –m pip install pyttsx3

Wait for the installation to complete.

CREATING A SPEECH PROGRAM:

  1. IMPORTING PYTTSX3:

Once you have installed pyttsx3, we can start with the fun part. We will now create a program that will be able to speak a sentence/phrase.

To do that, we must first import our freshly installed library to our new python file with the command,

import pyttsx3

  1. DEFINING THE FUNCTION:

Now, we will define a function that will accept a string as a parameter which will be then spoken by the program.

def speak(text):

We will create a variable named ‘engine’ which will call the initial function of the ‘pyttsx3’ module. 

engine = pyttsx3.init()

Now, all we need to do is use the ‘engine’ variable and call the method ‘say()’ which is a predefined method in pyttsx3. This method will convert any text to speech.

engine.say(text)

After this, we will add another line of code which will ensure that the program finishes speaking before moving onto the next command (if any).

engine.runAndWait()

  1. EXECUTING THE PROGRAM:

Before executing it, you must first ask the user on what it wants the program to say and then take the input that follows. Then call the ‘speak’ function passing the input as the parameter.

So, after the function just add the following lines of code:

text = input(“What do you want the computer to say?”)

speak(text)

Now, run the program and give it a sentence/word and….just wait for the computer to speak in its sexy computerized voice. 

You can run this program from the commandline or right from the IDE if it supports input. If you use Sublime Text like myself, then here is how you can take input in python programs using Sublime Text. 

CONCLUSION:

There you go, you have now created a function that can speak any text. All you need to do now, is run it whenever your friend comes over and show it off.

If your friend is not impressed by a speech program, don’t worry…..we will soon learn how to create a full-fledged virtual assistant and then, you might want to change your Truecaller name to ‘Tony Stark’ ;)

If you found this helpful, drop this article a like and share it with your friends and buddies who love Iron Man :D 

What was the first thing you wanted your computer to say? Was it ‘Welcome Back sir!’? Because that’s what I did, it really gave me that sense of achievement xD I would love to know your computer's first words in the comments below!

In the upcoming lessons, we will learn how to make our computer do certain tasks and we will also try and make our virtual assistant’s voice a bit better.

Looking for a programming community to join? Join Noob Code Pro’s Official Telegram Group today and get instant access to our chats, discussions and logic building exercises. Hope to see you there!!

I’ll be back next week with some new and fun knowledge to share with you until then……

HAVE AN AWESOME DAY!!!