Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

String-based python interview questions


A string is a collection of chars, we can store multiple characters set into String.

The base Index of String will be started from 0 to size-1 similar to LIST.but all elements of String is connected with each other.



we can write String is single quote (' '),double quote (" ") and triple quote (""").


a= 'hello'  ;  //Single Quote String

a = "hello"  //Double quote String

a= """hello""" //Triple quote String


s = "hello"

s[0]= h
s[1]= e
s[2]=l
s[3]=l
s[4]=o

Example of String Program?


Q1) WAP to count the total vowel and consonant in String?


Solution:-

s = "hello"
c1=0
c2=0
for i in range(0,len(s)):
    if s[i]=='a' or s[i]=='e' or s[i]=='i' or s[i]=='o' or s[i]=='u':
        c1=c1+1
    else:
        c2=c2+1
print("total vowel is ",c1," total consonent is ",c2)     



Q2)  WAP to check that String is palindrome or not?

s= input("enter string")  #aaaaaa
size= len(s)-1
c=1
for i in range(0,(len(s))//2):
    if s[i]!=s[size-i]:
        c=0
        print("not pallindrom")
        break

if c==1:
    print("pallindrom")


Q3)  WAP to find max char in String?


Solution:-
s = "hellop"
max=s[0]
for i in range(1,len(s)):
    if max<s[i]:
        max=s[i]

print(max)     



Q)WAP to validate email?

Solution:-
s = "@abcg.mailcom"
atpos=0
dotpos=0
for i in range(0,len(s)):
    if s[i]=="@" :
        atpos=i

    if s[i]=="." :
        dotpos=i


     
if atpos>0 and (dotpos-atpos)>=2:
     print("valid email id")

else:
    print("invalid email id")

Q) WAP to validate the mobile number?


Q) WAP to validate first name and last name?

Q) WAP to provide a weak password and a Strong password?



1234 //w

12345//w

S@$1234 //Strong

123s@$123 //Strong



Q) WAP to find the largest palindrome in String?


Q) WAP to find max char in each string in a word?


Q) WAP to replace white space with an underscore?


Q) WAP to count total upper case, lower case, number, special char in String?

s = "abCA@$_123"
sc=0
chkcase=0
for data in s:
    if data in('@','$','_','#','!'):
        sc=sc+1
    if ord(data)>=65 and ord(data)<=91:
        chkcase=chkcase+1

if len(s)>5 and sc>2 and chkcase>1:
    print("strong password")

else:
    print("weak password")

Q) WAP to reverse a string in words?

s= "C CPP DS Java PHP"  # C PPC  SD  avaJ  PHP

s = s + " "
x = []
data = ''
for i in range(0,len(s)):
    if s[i]!=' ':
        data = data+s[i]
    else:
        x.append(data)
        data=''


for i in range(0,len(x)):
    d = x[i]
    
    for j in range(len(d)-1,-1,-1):
        print(d[j],end='')
    print()  


Predefine method of String in Python:-

MethodDescription

Python String capitalize() Converts first character to Capital Letter
Python String center() Pads string with the specified character
Python String casefold() converts to case folded strings
Python String count() returns occurrences of a substring in a string
Python String endswith() Checks if String Ends with the Specified Suffix
Python String expandtabs() Replaces Tab character With Spaces
Python String encode() returns encoded string of given string
Python String find() Returns the index of the first occurrence of a substring
Python String format() formats string into the nicer output
Python String index() Returns Index of Substring
Python String isalnum() Checks Alphanumeric Character
Python String isalpha() Checks if All Characters are Alphabets
Python String isdecimal() Checks Decimal Characters
Python String isdigit() Checks Digit Characters
Python String isidentifier() Checks for Valid Identifier
Python String islower() Checks if all Alphabets in a String are Lowercase
Python String isnumeric() Checks Numeric Characters
Python String isprintable() Checks Printable Character
Python String isspace() Checks Whitespace Characters
Python String istitle() Checks for Titlecased String
Python String isupper() returns if all characters are uppercase characters
Python String join() Returns a Concatenated String
Python String ljust() returns a left-justified string of given width
Python String rjust() returns a right-justified string of given width
Python String lower() returns the lowercased string
Python String upper() returns an uppercased string
Python String swapcase() swap uppercase characters to lowercase; vice versa
Python String lstrip() Removes Leading Characters
Python String rstrip() Removes Trailing Characters
Python String strip() Removes Both Leading and Trailing Characters
Python String partition() Returns a Tuple
Python String maketrans() returns a translation table
Python String rpartition() Returns a Tuple
Python String translate() returns mapped charactered string
Python String replace() Replaces Substring Inside
Python String rfind() Returns the Highest Index of Substring
Python String rindex() Returns Highest Index of Substring
Python String split() Splits String from Left
Python String rsplit() Splits String From Right
Python String splitlines() Splits String at Line Boundaries
Python String startswith() Checks if String Starts with the Specified String
Python String title() Returns a Title Cased String
Python String zfill() Returns a copy of The String Padded With Zeros
Python String format_map() Formats the String Using Dictionary
Python any() Checks if any Element of an Iterable is True
Python all() returns true when all elements in iterable is true
Python ascii() Returns String Containing Printable Representation
Python bool() Converts a Value to Boolean
Python bytearray() returns an array of given byte size
Python bytes() returns an immutable bytes object
Python compile() Returns a Python code object
Python complex() Creates a Complex Number
Python enumerate() Returns an Enumerate Object
Python filter() constructs iterator from elements that are true
Python float() returns floating-point number from number, string
Python input() reads and returns a line of string
Python int() returns an integer from a number or string
Python iter() returns an iterator for an object
Python len() Returns Length of an Object
Python max() returns largest element
Python min() returns smallest element
Python map() Applies Function and Returns a List
Python ord() returns Unicode code point for Unicode character
Python reversed() returns reversed iterator of a sequence
Python slice() creates a slice object specified by range()
Python sorted() returns a sorted list from a given iterable
Python sum() Add items of an Iterable
Python zip() Returns an Iterator of Tuples


14 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

  1. s = "ssssss ram madam jjjjjjjjjj kkk ppp ram hari sdafdfsafds dzfv"
    s1= s+ " ";
    lst=[]
    data=''
    for i in range(0,len(s1)):

    if s1[i]!= " ":
    data=data+s1[i]
    else:
    lst.append(data)
    data=''


    #print(lst)
    m=0
    for i in range(0,len(lst)):
    s2=lst[i]
    for j in range (0 ,len(s2)//2):
    if s2[j]!=s2[(len(s2)-1)-j]:
    break
    else:
    if m<len(s2):
    m=len(s2)
    index=i



    print(lst[index])



    ReplyDelete
  2. WAP to validate the mobile number

    s=input("mobile number")
    if len(s)==10 and s[0]!="0":
    print("valid")
    else:
    print("inavlid")

    ReplyDelete
  3. WAP to validate first name and last name

    a=input("enter full name")
    c=0
    for i in range(0,len(a)):
    if a[i].isspace():
    c=c+1
    if c==1 and a[0].isspace()==False:

    print("valid name")
    else:
    print("invalid name")

    ReplyDelete
  4. WAP to provide a weak password and Strong password

    p=input("enter password")
    d=0
    u=0
    l=0
    s=["@","#","*","&","^","$","!"]
    sc=0
    for i in p:
    if i in s:
    sc=sc+1
    if i.isdigit():
    d=d+1
    if i.isupper():
    u=u+1
    if i.islower():
    l=l+1
    if len(p)>7 and sc>0 and d>0 and u>0 and l>0:
    print("strong password")
    else:
    print("weak password")

    ReplyDelete
  5. WAP to find the largest palindrome in String

    a=input("enter palindrome")
    s=a.split(" ")
    max1=""
    for i in s:
    if len(i)>len(max1):
    max1=i
    else:
    continue
    if max1[::-1]==max1:
    print("longest palindrome is",max1)
    else:
    print("no")

    ReplyDelete
  6. WAP to find max char in each string in a word

    a=input("enter word")
    m=65
    for i in a:
    if ord(i)>m:
    m=ord(i)
    print(chr(m))

    ReplyDelete
  7. WAP to replace white space with an underscore

    a=input("enter name")
    s=""
    for i in range(0,len(a)):
    if a[i].isspace():
    s=s+"_"
    else:
    s=s+a[i]

    print(s)

    ReplyDelete
  8. WAP to count total upper case, lower case, number, special char in String

    a=input("enter word")
    d=0
    u=0
    l=0
    s=0
    for i in a:
    if i.isdigit():
    d=d+1
    elif i.isupper():
    u=u+1
    elif i.islower():
    l=l+1
    else:
    s=s+1
    print(d,u,l,s)

    ReplyDelete
  9. WAP to reverse a string in words

    a=input("enter words")
    l=a.split(" ")
    l=l[::-1]
    s=""
    for i in l:
    s=s+i+" "
    print(s)

    ReplyDelete
  10. #password strenght
    s=input("enter password")
    c=0
    for i in range(0,len(s)):
    if ord(s[i])>=48 and ord(s[i])<=57:
    c=c+1
    if ord(s[i])>=65 and ord(s[i])<=90:
    c=c+1
    if ord(s[i])>=97 and ord(s[i])<=122:
    c=c+1
    if ord(s[i])>=58 and ord(s[i])<=64:
    c=c+1
    if c>15:
    print("strong password")
    if c>=10 and c<=15:
    print("medium password")
    if c>6 and c<10:
    print("weak password")
    if c<6:
    print("password should be greater than 6 character")








    ReplyDelete
  11. x=input("enter mobile number")
    c=0
    for i in range(0,len(x)):
    if ord(x[i])>=48 and ord(x[i])<=57:
    c=c+1
    if c==10:
    print("valid mobile number")
    else:
    print("invalid mobile no")

    ReplyDelete
  12. fn=input("enter user name")
    ln=input("enter your last name")
    for i in range(0,len(fn)):
    if (ord(fn[i])>=65 and ord(fn[i])<=90) or (ord(fn[i])>=97 and ord(fn[i])<=122):
    print("valid first name")
    break;
    else:
    print("invalid first name")
    break;
    for j in range(0,len(ln)):
    if (ord(ln[j])>=65 and ord(ln[j])<=90) or (ord(ln[j])>=97 and ord(ln[j])<=122):
    print("valid last name")
    break;
    else:
    print("invalid last name")
    break;

    ReplyDelete
  13. #max in string
    s=input("enter a string")
    m="0"
    for i in s:
    if m<i:
    m=i
    print(m)

    ReplyDelete
  14. replace space with @
    x="gourav ji"
    m=' '
    for i in range(len(x)):
    if (x[i])==' ':
    m=m+'@'
    else:
    m=m+x[i]
    print(m)

    ReplyDelete

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post