How to Generate a Company Email from Full Name in Python

👋 Diving into Python? Here’s a beginner-friendly tutorial on a simple script that generates a company email from a full name using command-line arguments. I’ll break it down step by step, showing how it handles inputs, formats strings, and outputs results. Great for learning sys.argv and string manipulation! What Does This Script Do? It takes a full name (e.g., “Kannan Srinivasan”) as a command-line argument, converts it into a company email format (lowercase with dots instead of spaces, like “kannan.srinivasan@company.com”), and prints a neat profile output. The Script Here’s the Python code. Save it as email_generator.py and run it from your terminal! import sys # Check for sufficient arguments if len(sys.argv) < 2: print("Usage: python email_generator.py 'Full Name'") sys.exit() # Combine arguments into full name full_name = " ".join(sys.argv[1:]) # Print the full name for verification print(full_name) # Format the name into an email email = full_name.lower().replace(old=" ", new=".") + "@company.com" # Output the profile print("\n--- Your Profile ---") print("Name:", full_name) print("Email:", email) Step-by-Step Explanation for Beginners Let’s break it down line by line—explaining each part: 1 Import the Module: ◦ import sys: Imports sys module for sys.argv, a list of command-line arguments (e.g., python email_generator.py Kannan Srinivasan). Essential for inputs. 2 Check for Inputs: ◦ if len(sys.argv) < 2:: sys.argv starts with script at 0; <2 means no args. Prints usage and exits. Ensures input. 3 Build the Full Name: ◦ full_name = " ".join(sys.argv[1:]): Joins args from 1 with spaces into string. Handles multiple words. 4 Print the Name: ◦ print(full_name): Displays name. Verifies input. 5 Format the Email: ◦ email = full_name.lower().replace(old=" ", new=".") + "@company.com": ▪ .lower(): To lowercase (e.g., “Kannan Srinivasan” → “kannan srinivasan”). ▪ .replace(old=" ", new="."): Spaces to dots (→ “kannan.srinivasan”). ▪ + "@company.com": Adds domain. 6 Output the Profile: ◦ print("\n--- Your Profile ---"): Header with newline. ◦ print("Name:", full_name) and print("Email:", email): Displays results. How to Run It Run in terminal:
python email_generator.py Kannan Srinivasan Expected Output: Kannan Srinivasan --- Your Profile --- Name: Kannan Srinivasan Email: kannan.srinivasan@company.com No name shows usage message. Quick Tips for Beginners • Covers: Command-line args (sys.argv), string methods (lower(), replace(), join()), error checking. • Extend: Add custom domains or special character handling. • Pro tip: For names with spaces, run without quotes or quote the name (e.g., "Kannan Srinivasan"). #PythonBeginners #CodingTips #TechTutorial #LearnPython

To view or add a comment, sign in

Explore content categories