Computer Science, asked by AbhijithPrakash, 11 months ago

Computer Science Class 11

Language: Python


1. Write a program that reads a string and then prints a string that capitalizes every other letter in the string e.g., passion becomes pAsSiOn.

2. Write a program that reads email-id of a person in the form of a string and ensures that it belongs to domain @brainly.com. (Assumption: No invalid characters are there in email-id).

Answers

Answered by sswaraj04
13

Answer:

var=input("Enter your email-id: ")

st='@brainly.com'

if(var.find(st)==-1):

   print("Wrong domain")

else:

   print("It's right")

***************************************************

var=input("Enter string: ")

newvar=''

n=0

for a in var:

   if n%2!=0:

       k=a.upper()

   else:

       k=a

   newvar+=k

   n+=1

print(newvar)

Explanation:

find function directly finds given substring in given large string

for changing case a variable is checked for even places and capitalize it

and nothing changes at odd places(index start at 0)

and adding each character to a new string and then print the new string

Hope it helps :-)


AbhijithPrakash: Great!!
Answered by fiercespartan
24

Let me tell you that there are more than 1 solution to solve any of this problem. How are they all different? One solution might be faster than the others or one might be more accurate. Hence, there is one thing to decide upon. Which method will you be choosing? A method which takes less time to write or method which is highly accurate. All the codes work but there always is a code which better than the other in various ways.

Since this is a 11th grade question, I personally be choosing more accuracy rather than speed. It all depends since I am not a 11th grader. But I will be proving various codes for one problem. It is your decision to choose one.

________________________________________________

PROBLEM 1:

In this problem, we are asked to capitalize every alternate letter in a word.

passion → pAsSiOn

letter → lEtTeR

Now, let's look at this with indexes:

So we know that the 1st index is not capitalized, so index 0 is not upper, index 1 is upper, index 2 is not upper but index 3 is upper.

What do we notice?

Every odd index an upper case and every even index should be left as is.

Here are two ways that I can think of approaching this problem:

CODE 1:

sentence = input('Enter your string:')

sentence = list(sentence)

for i in range(len(sentence)):

   if 1%2 != 0:

       sentence[i] = sentence[i].upper()

print(''.join(sentence))

Coders generally tend to use replace for these sort of problems but remember that replace() replaces all the occurrences of a character in a string. If we want to replace 'b' with 'B'. replace() replaces b with B for all the b's even if the 'b' is in a even index position. So, it is not accurate.

CODE 2:

sentence = input('Enter your string:')

sentence = list(sentence)

for i in range(1,len(sentence),2):

   word[i] == word[i].upper()

print(''.join(sentence))

_________________________________________________

PROBLEM 2:

Given an email address, we will need to see if '@brainly.com' exsists. So, the mail's domain shall be '@brainly.com'

Here are the several ways:

CODE 1:

email = input('Input your email address:')

if '@brainly.com' in email:

   print('Valid')

else:

   print('Not valid')

CODE 2:

email = input('Enter your email address:')

email = email.split('@')

if email[-1] == 'brainly.com':

   print('Valid')

else:

   print('Not valid')

_______________________________________________


AbhijithPrakash: Awesome!!
Similar questions