Working with String / String Functions in Python

 Working with String / String Functions in Python

In Python there are various methods used for working with strings

1. Str.upper():

This is built in function in Python used to print given string in upper case. 

For example,

my_name=" dr.manisha more"

print(str.upper(my_name))

O/P :

DR.MANISHA MORE

2. Str.lower():

Str.lower() is used to display string in lower case format. 

For example,

lower_method="HELLO STUDENTS"

print(str.lower(lower_method))

O/P:

hello students

3. Str.find(substring):

Str.find() function is used to find the index of occurance of character from given string. It gives index number. Index start from 0. If character not found it return -1.

For example,

my_text="India is my country"

substring=my_text.find("country")

print(substring)

O/P:

12

my_text="India is my country"

substring=my_text.find("song")

print(substring)

O/P:

-1

4. str.replace(old,new):


Str.replace() method is used to replace the old string with new string from given string.

For example,

text=" This is Python lecture on String literal"

new_text=text.replace("lecture","Session")

print(new_text)

O/P:

This is Python Session on String literal


5.Str.split(delimiter):

This method returns given string with given delimiter.

For example,

text="Python is very simple language. Python is easy to understand and learn"

#new_text=text.split(sep=" ")

new_text=text.split(" ")

print(new_text)


O/P:

['Python', 'is', 'very', 'simple', 'language.', 'Python', 'is', 'easy', 'to', 'understand', 'and', 'learn']


6. Str.strip():

Str.strip() function returns the string by removing all leading and trailing white spaces. 

For example,

text1= '''      Hi students How are You? Are you able to write   ?

please listen

focus on studies  give time for practice '''

print("Original Text\n")

print(text1)

print("\nAfter removing leading spaces\n")

text2=text1.strip()

print(text2)


O/P:

Original Text


      Hi students How are You? Are you able to write   ?

please listen

focus on studies  give time for practice 


After removing leading spaces

Hi students How are You? Are you able to write   ?

please listen

focus on studies  give time for practice

7. Len(str):

Len(str) function is used to get length of string. 

For example,

text="Hello students, How are you?"

len(text)

O/P:

28

8. Str.join():

Str.join() method of string is used to concatenate the strings with a specified separator or delimiter. Just call the join () method on delimeter.

For example,

text2= "This is Python lecture and demonstration on String literal"

delimiter= " "

joined_string=delimiter.join(text2)

print(joined_string)

O/P:

T h i s   i s   P y t h o n   l e c t u r e   a n d   d e m o n s t r a t i o n   o n   S t r i n g   l i t e r a l



Another example,

list1=["students", "please", "practice", "Python", "programs"]

result=" , ".join(list1)

print(result)

O/P:

students , please , practice , Python , programs


How to format string in Python:

Format():

a=10

b=20

c=a+b

print("The addition of {} and {} is: {}".format(a,b,c))

Output:

The addition of 10 and 20 is: 30


F Character:

a1=10

b1=20

c1=a+b

print(f"The addition of {a1} and {b1} is: {c1}")


Output:

The addition of 10 and 20 is: 30


+ Character:

my_class="BCA"

my_rollno=12

print("My class is "+my_class+ " and my roll number is "+str(my_rollno))

Output:

My class is BCA and my roll number is 12


टिप्पणी पोस्ट करा

0 टिप्पण्या