Header Ads Widget

Responsive Advertisement

String in python and important methods with programme


Hello students Here we come with new topic String. String is the collection of character and collection of words. like A word and A sentence or may be a paragraph. Here we define some important string function which is used on String and all this function always return a new value not change original value. We define some string methods and its definition.

python


Read all these method and See output of each programme and each  method

MethodDescription
capitalize()Converts the first character to upper case
casefold()Converts string into lower case
center()Returns a centered string
count()Returns the number of times a specified value occurs in a string
encode()Returns an encoded version of the string
endswith()Returns true if the string ends with the specified value
expandtabs()Sets the tab size of the string
find()Searches the string for a specified value and returns the position of where it was found
format()Formats specified values in a string
format_map()Formats specified values in a string
index()Searches the string for a specified value and returns the position of where it was found
isalnum()Returns True if all characters in the string are alphanumeric
isalpha()Returns True if all characters in the string are in the alphabet
isascii()Returns True if all characters in the string are ascii characters
isdecimal()Returns True if all characters in the string are decimals
isdigit()Returns True if all characters in the string are digits
isidentifier()Returns True if the string is an identifier
islower()Returns True if all characters in the string are lower case
isnumeric()Returns True if all characters in the string are numeric
isprintable()Returns True if all characters in the string are printable
isspace()Returns True if all characters in the string are whitespaces
istitle()Returns True if the string follows the rules of a title
isupper()Returns True if all characters in the string are upper case
join()Converts the elements of an iterable into a string
ljust()Returns a left justified version of the string
lower()Converts a string into lower case
lstrip()Returns a left trim version of the string
maketrans()Returns a translation table to be used in translations
partition()Returns a tuple where the string is parted into three parts
replace()Returns a string where a specified value is replaced with a specified value
rfind()Searches the string for a specified value and returns the last position of where it was found
rindex()Searches the string for a specified value and returns the last position of where it was found
rjust()Returns a right justified version of the string
rpartition()Returns a tuple where the string is parted into three parts
rsplit()Splits the string at the specified separator, and returns a list
rstrip()Returns a right trim version of the string
split()Splits the string at the specified separator, and returns a list
splitlines()Splits the string at line breaks and returns a list
startswith()Returns true if the string starts with the specified value
strip()Returns a trimmed version of the string
swapcase()Swaps cases, lower case becomes upper case and vice versa
title()Converts the first character of each word to upper case
translate()Returns a translated string
upper()Converts a string into upper case
zfill()Fills the string with a specified number of 0 values at the beginning


You can use three double quotes:


a = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
print(a)
 

a = '''Lorem ipsum dolor sit amet,

consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.'

consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.'''
print(a)


a = "Hello, World!"
print(a[1])


a = "Hello, World!"
print(len(a))


txt = "The best things in life are free!"
print("free" in txt)


txt = "The best things in life are free!"
print("expensive" not in txt)


txt = "The best things in life are free!"
if "expensive" not in txt:
  print("No, 'expensive' is NOT present.")

Get the characters from position 2 to position 5 (not included):
b = "Hello, World!"
print(b[2:5])

Get the characters from the start to position 5 (not included):
b = "Hello, World!"
print(b[:5])

Get the characters from position 2, and all the way to the end:
b = "Hello, World!"
print(b[2:])

a = "Hello, World!"
print(a.upper())


a = "Hello, World!"
print(a.lower())


a = " Hello, World! "
print(a.strip()) # returns "Hello, World!"


a = "Hello, World!"
print(a.replace("H""J"))


a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']


a = "Hello"
b = "World"
c = a + b
print(c)


a = "Hello"
b = "World"
c = a + " " + b
print(c)



age = 36
txt = "My name is John, and I am {}"
print(txt.format(age)

quantity = 3
itemno = 567
price = 49.95
myorder = "I want {} pieces of item {} for {} dollars."
print(myorder.format(quantity, itemno, price))


quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {2} dollars for {0} pieces of item {1}."
print(myorder.format(quantity, itemno, price))


print(txt = "We are the so-called \"Vikings\" from the north.")


CodeResult
\'Single Quote
\\Backslash
\nNew Line
\rCarriage Return
\tTab
\bBackspace




Get the characters:
From: "o" in "World!" (position -5)
To, but not included: "d" in "World!" (position -2):
b = "Hello, World!"
print(b[-5:-2])



a='''consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.'''
print(a)


a = "Hello, World!"
print(a[1])


a = "Hello, World!"
print(len(a))


txt = "The best things in life are free!"
print("free" in txt)


txt = "The best things in life are free!"
print("expensive" not in txt)


txt = "The best things in life are free!"
if "expensive" not in txt:
  print("No, 'expensive' is NOT present.")

Get the characters from position 2 to position 5 (not included):
b = "Hello, World!"
print(b[2:5])

Get the characters from the start to position 5 (not included):
b = "Hello, World!"
print(b[:5])

Get the characters from position 2, and all the way to the end:
b = "Hello, World!"
print(b[2:])

Get the characters:
From: "o" in "World!" (position -5)
To, but not included: "d" in "World!" (position -2):
b = "Hello, World!"
print(b[-5:-2])




Q1. Write a program to accept a string and display its length. 
Ans. 
str = input("Enter any String") 
print(len(str))


Q2. Write a program to accept a string and count number of vowels. 
Ans. 
str = input("Enter any String") 
s1="aeiouAEIOU" 
v=0 
for i in str: if i in s1: 
v=v+1 print(v)


Q3. Write a program to accept a string and display it in reverse. 
Ans 
str = input("Enter any String") print(str[: :-1]) 


Q4. Write a program to check whether an input string is palindrome or not.
Ans. 
str = input("Enter any String") 
strrev ="" 
for i in range(len(str),0,-1): 
strrev=strrev + str[i-1] 
if str.lower() == strrev.lower(): 
print("Palindrome") 
else: print("Not a palindrome") 



Q5. Write a program to count number of small case alphabets in String. 
Ans. 
str = input("Enter any String") 
L=0 
for i in str: 
if i.islower( ): 
L = L+1 
print("Total small case alphabets are : ",L)


Q6. Write a program to count the number of words in the input string. 
Ans. 
str = input("Enter any String") 
L=str.split() 
print("Total number of words are ", len(L)) 



Q7. Write a program to accept a string from the user and count the frequency of alphabet ‘a’ and ‘c’. 
Ans. 
str = input("Enter any String") 
a=0 
c=0 
for i in str: 
if i=='a': 
a+=1 
if i=='c': 
c+=1 
print("Total number of alphabet 'a' is ", a) 
print("Total number of alphabet 'c' is ", c) 


Q8. Write a program to display the last word of the string accepted from user. 
Ans. 
str = input("Enter any String") 
w=str.split() 
print(w[-1]) 



Q9. Write a program to display those words from the string in python which are starting from alphabet ‘a’ or ‘A”.
Ans. 
str = input("Enter any String") 
w=str.split() 
c=0 
for i in w: 
if i[0]=='a' or i[0]=='A': 
c=c+1 
print("Total words starting from 'a' or 'A' : ",c) 



Q10. Write a program to count number of digits in a string accepted from user. 
Ans. 
str = input("Enter any String") 
d=0 for i in str: 
if i.isdigit(): 
d=d+1 
print("Total digits are : ",d)


Download String Complete Notes















Post a Comment

0 Comments