Python code to translate English to German

Python can be used to translate human language. Python’s googletrans library uses Google Translate API to translate text. To install this library, type the following command:

pip install googletrans

Following code translates English text to German text:

from googletrans import Translator

# Initialize the translator
translator = Translator(service_urls=['translate.google.com'])

# Define the text to be translated
text = "Hello, how are you?"

# Translate the text
result = translator.translate(text, dest='de')

# Print the translated text
print(result.text)

Output of this code is:

Hallo, wie geht es dir?