Python Core



Admission Enquiry Form



Strings in Python.

A String is the collection of characters. In Python the string is kept in single quotes or double quotes.

Example of String in Python:

s1='Welcome to Compuhelp'    #Assigning string to variable s1.
s2="Welcome to Compuhelp"  #Assigning string to variable s2.




Assigning and printing string.

s1='Welcome to Compuhelp'    #Assigning string to variable s1.
s2="Welcome to Compuhelp"  #Assigning string to variable s2.

print(s1)
print(s2)




Assigning multiline string to a variable.

Multiline string can be assigned to a variable using three single quotes or three double qoutes.

#Assigning multiline string to a variable.
s1="""Welcome to compuhelp
thanks to join us"""
print(s1)


Output:

Welcome to compuhelp
    thanks to join us


Counting String Length using len() method.

s1='Compuhelp'
n=len(s1)
print(n)


Output:

9


String Slicing:

A slice represents a part or piece of a string. The syntax of the slicing is:

stringname[start:stop:stepsize]

s1="Compuhelp"
print(s1[5:9])


Output:

help


String slicing With stepsize 2

s1="Compuhelp"
print(s1[0:9:2])


Output:

Cmuep


#Access string from 0th to last character.

s1="Compuhelp"
print(s1[:])
print(s1[::])


Output:

Compuhelp
             Compuhelp


#Using start and stop as negative index numbers.

s1="Compuhelp"
print(s1[-4:-1])


Output:

hel


Repeating the strings.

#Repeating the strings.

s1="Compuhelp"
print(s1*2)


Output:

CompuhelpCompuhelp


Concatenation of strings.

#Concatenation of Strings
s1="Compu"
s2="help"
s3=s1+s2 #concatenate s1 and s2
print(s3) #display the concatenated string


Output:

Compuhelp


Using membership operator in

#Checking Membership using in operator
s1="Compuhelp"
print("pu" in s1)


Output: True



Using membership operator not in.

#Checking Membership using not in operator
s1="Compuhelp"
print("pu" not in s1)

Output: False

#Comparing strings
s1="Compuhelp"
s2="Compuhelp"
print(s1==s2)

Output: True



Replacing a string with another string using replace() method.

Syntax: stringname.replace(oldstring,newstring)

#Replacing a string with another string.

s1="ComputerHelp"
str1=s1.replace("Computer","Compu")
print(str1)

Output: Compuhelp



upper() method:

This method changes the string from lower case to upper case

#Changing the string from lowercase to uppercase.

s1="compuhelp"
print(s1.upper())

Output: COMPUHELP



lower() method:

This method changes the string from upper case to lower case

#Changing the string from uppercase to lowercase.

s1="COMPUHELP"
print(s1.lower())

Output: compuhelp



isalnum() method:

This method returns True if all characters in the string are alphanumeric(A to Z, a to z, 0 to 9).

#using isalnum() method.

s1="Compuhelp123"
print(s1.isalnum())

Output: True

#using isalnum() method with special characters in string.

s1="Compuhelp@#"
print(s1.isalnum())

Output: False



isalpha() method:

This method returns True if all characters in the string are alphabetic(A to Z, a to z).

#using isalpha() method.

s1="Compuhelp"
print(s1.isalpha())

Output: True

#using isalpha() method with integers.

s1="Compuhelp123"
print(s1.isalpha())

Output: False



isdigit() method:

This method returns True if the string contains only numeric digits(0 to 9).

#using isdigit() method.

s1="12345"
print(s1.isdigit())

Output: True

#using isdigit() method with alphabets in string.

s1="Compuhelp123"
print(s1.isdigit())

Output: False



islower() method:

This method returns True if all characters in the string are in lower case.

#using islower() method.

s1="compuhelp"
print(s1.islower())

Output: True



isupper() method:

This method returns True if all characters in the string are in upper case.

#using isupper() method

s1="COMPUHELP"
print(s1.isupper())

Output: True



istitle() method:

This method returns True if each word of the string starts with capital letter.

#using of istitle() method.

s1="Welcome To Compuhelp"
print(s1.istitle())

Output: True



isspace() method:

This method returns True if the string contains only spaces.

#using of isspace() method

s1="       "
print(s1.isspace())

Output: True



isspace() method:

This method returns True if the string contains only spaces.

#using of isspace() method

s1="       "
print(s1.isspace())

Output: True

String Practice :

          PYTHON STRING PRACTICE 
#use of string functions
mystr="compuhelp makes easy to understand python"
length=len(mystr)
print("the length of the string str  is ",len(mystr))
print("using slicing",mystr[0:5])
print("using slicing :",mystr[5:])#prints all characters from 5th character
print("using slicing :",mystr[5:9])#will print help
print(mystr[-6])#prints p from last to 5th character
print(mystr[5:-12])#starts printing from 6th character left side till 12 character from right side
                  # output :help makes easy to under
for i in mystr: #will print whole string by inserting ,(comma) in each character
   print(i,end=",")
count=len(mystr)
print(count)
while(count>0):
   print(mystr[count-1],end=" ")
   count=count-1
#output is :n o h t y p   d n a t s r e d n u   o t   y s a e   s e k a m   p l e h u p m o
str_vowels=""
str_consonants=""
for x in mystr:
   if x=='a' or x=='e' or x=='i' or x=='o' or x=='u':
       str_vowels=str_vowels + x + ","
       #print(x,end=" ")
   else:
       str_consonants=str_consonants+x
       #print(x,end=",")
print("Vowels are :",str_vowels)
print("Consonants are:",str_consonants)