Home › Interview Questions › How would you extract the part of a string before …

How would you extract the part of a string before the dot in a domain name, such as 'example.com'?

🟢 Easy Coding Fresher level
1Times asked
Oct 2026Last seen
Oct 2026First seen

💡 Model Answer

To extract the part of a string before the dot in a domain name, you can split the string on the dot and take the first element. In Python:

python
domain = 'example.com'
prefix = domain.split('.')[0]
print(prefix)  # outputs 'example'

If you need to handle subdomains or multiple dots, you can use rsplit('.', 1) to split from the rightmost dot:

python
subdomain = 'mail.example.com'
prefix = subdomain.rsplit('.', 1)[0]
print(prefix)  # outputs 'mail.example'

Alternatively, a regular expression can capture everything before the first dot:

python
import re
match = re.match(r'^([^\.]+)', domain)
prefix = match.group(1) if match else ''

These approaches work in most programming languages with similar string manipulation functions.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500