Chapter - 6 (String)
For learn how to download and install python link here: How to download and install python in window
For learn how to run and execute the code in python link here:How to write and run the code
In this blog, the format is first I write Code then Result of that code. You can directly copy the code and so your time save in written that code. Also , image of the code and result and after which it contain explanation of the code.
Example-1: Where use eval where not
Code:
num=eval(input('Enter a number:'))
string=input('Enter a string:')Explanation:- We use eval code in first chapter but here we not use because eval use for calculation purpose in number but here string( means alphabet) where we not use calculation.
Example-2: Write a program in which ask user to enter a letter and if it contains vowel then print the vowel.
Code:
s = ''
for i in range(5):
t = input('Enter a letter: ')
if t=='a' or t=='e' or t=='i' or t=='o' or t=='u':
s = s + t
print('Vowel in the string',s)
Result:
Enter a letter: s
Enter a letter: w
Enter a letter: a
Enter a letter: e
Enter a letter: t
Vowel in the string ae
Explanation:- In the first line you see s='' it means first string is empty and we add word in it.
Then in 2nd line we use for loop which repeat 5 times and in 3rd line take the input data from the user.
In the 4th line we use command if the data is equal to either a,e,i,o,u.
In 5th line use s=s+t it means we add the add the alphabet in the string which is empty first and finally show the answer.
Example-3: Write a program in which ask user to enter a letter and if it contains vowel then print the vowel.
Code:s=''
for i in range(5):
t=input('Enter a letter:')
if t in 'aeiou':
s=s+t
print(s)
Result:
Enter a letter: a
Enter a letter: t
Enter a letter: e
Enter a letter: f
Enter a letter: g
ae
Explanation:- It is similar to previous example but it shortcut of that example if you want to reduce time and make the code small then use this directly you understand it easily.
Example-4: Write a program and ask the user to find either letter 'a' is present in it.
Code:t = input('Enter a letter: ')
if 'a' in t:
print('a letter present in the string')
Result:
Enter a letter: elephant
a letter present in the string
Explanation:- In the 1st line we take input data from user.
In 2nd line we use command if 'a' in t, as per name it suggest if a letter present in input then print it.
Example-5: Write a program and ask the user to find either letter 'a' is not present in it.
Code:t = input('Enter a letter: ')
if 'a' in t:
print('a letter not present in the string')
Result:
Enter a letter: guru
a letter not present in the string
Explanation:- In the 1st line we take input data from user.
In 2nd line we use command if 'a' not in t, as per name it suggest if a letter not present in input then print it.
Example-6: Some important expression
Example-7: If we take example as 'Python' and put a letter 'a' in between the 't' and 'h'
Code:s='Python'
print(s[:3] + 'p' + s[3:])
Result:
Pytahon
Explanation:- In the 1st line you see the example
In the 2nd line you see s[:3] it means s=python and if double dot before 3 it means before the 3rd position and then add 'a' and after double dot before 3 it means after the 3rd position and then close the bracket.
Example-8: To calculate the length of a word
Code:x=input('Enter a word:')
print(len(x))
Result:
Enter a word:guru
4
Explanation:- In this you see 'len' command which calculate the length of word and use this before the word which length you want to calculate.
Example-9: Some important expression
Example-10: Import string command
To get more string help download this file:String command
Code:
help(str)
Result:Click on squeeze text to get str command
Example-11: Write a program and ask the user for the input and print all input into uppercase letter.
Code:
s = input('Enter a string:')
print(s.upper())
Result:
Enter a string:python
PYTHON
Explanation:- In the 2nd line you see command 's.upper()' it means change the letter of 's' into uppercase letter.
If you use here s.lower() then all change into lowercase.
Example-12: Print the word into next line.
Code:
s= 'Hi \nThere'
print(s)
Result:
Hi
There
Explanation:- Here we use backslash(\) and n i.e., \n to print in the next line.
Example-13: Print the word into next to next line.
Code:
s='Hi \n\nThere'
print(s)
Result:
Hi
There
Explanation:- Here we use two time \n\n to print in next to next line.
Example-14: To use apostrophe (') in between the string.
Code:
s= 'I can\'t do'
print(s)
Result:
I can't do
Explanation:- Here we use backslash and then apostrophe. If you use without using backslash then it close and it. You can check yourself.
Example-15: To use apostrophe (') in between the string.
Code:
a="I can't do"
print(a)
Result:
I can't do
Explanation:- You can use this also instead of code use in previous example. This is the another way to use apostrophe . You use in Double quote (") before and end of the string.
Example-16: To print ten blank line
Code:
print('\n'*9)
Result:
Explanation:- In Result you see blank it means ten blank line print on the result side.
Example-17: Write a program and ask user to enter input and if contain 'a' then print the location where a exist.
Code:
s = input('Enter some text: ')
for i in range(len(s)):
if s[i]=='a':
print(i)
Result:
Enter some text: abcdefa
0
6
Explanation:- In 1st line we ask input from the user.
In 2nd line you for loop which repeat according to length of the input user enter.
In 3rd line you see 's[i]' it means all letter present in string is equal to a, then print their position.
Example-18: Write a program and ask user for input and double all these letter.
Code:
s=input('Enter some text:')
double=''
for c in s:
double = double +c*2
print(double)
Result:
Enter some text: Hello
HH
HHee
HHeell
HHeellll
HHeelllloo
Explanation:- In 1st line we ask for input
In 2nd line we add an empty string
In 3rd line for c in s, Understand it carefully in this 'c' denote the single alphabet present in string, Means first is 'H' then in next line you see c*2 it means it double this letter.
But due to for loop and double= double +c*2 , in it you add all these letter one by one.
Example-19: Write a program in which ask user for input and print pattern like this 'H He Hel Hell Hello'
Code:
name= input('Enter your name:')
for i in range(len(name)):
print(name[:i+1], end='')
Result:
Enter your name: Hello
H He Hel Hell Hello
Explanation:- In 2nd line you see len command which is used to repeat the loop according to the string you write, Here you use Hello it means loop repeat for 5 times.
Then In 3rd line you see square bracket and [i+1] it means add letter one by one.
Example-20: Some important expression
Example-21: Write a program and remove punctuation from the input
Code:
s=input('Enter a word with semicolon')
for c in ';':
a = s.replace(c, '')
print(a)
Result:
Enter a word with semicolon python;python
pythonpython
Explanation:-Here we use replace command to replace semicolon without space.
In 2nd line you for c in ';' , in this c denote the single word present in the string .
Example-22: Write a program and print data after decimal(.)
Code:
s= input('Enter a decimal number:')
print(s.[s.index('.')+1:])
Result:
Enter a decimal number: 444.411649
411649
Explanation:- In this we use index command and add '.' in it, it means it index the place of decimal in the input and then use 1: means after the postion of decimal print all the data.
Example-23: Write a program and ask the user for input and print the secret message opposite to that message.
Code:
alphabet= 'abcdefghijklmnopqrstuvwxyz'
key= 'xznlwebgjhqdyvtkfuompciasr'
message=input('Enter your message:')
message= message.lower()
for c in message:
if c.isalpha():
print(key[alphabet.index(c)],end='')
else:
print(c, end='')
Result:
Enter your message: Hello
gwddt
Explanation:- In 1st line you see the alphabet and 2nd line key i.e,. secret message
In the 3rd line we ask for input
In the 4th line we lower all the letter which user either type or may be not
In the 5th line you see for c in message: , Here c denote the single alphabaet in message.
In the 6th line you see isalpha() command that is use for find wheter the data contain alphabaet or not
In the 7th line, we see first alphabet.index(c) it means index the all word in alphabet and then infront of it we use key that close in square bracket it means index of the word of alphabet in key and print according to their place.
No comments:
Post a Comment
If you have any doubt, let me know.