Responsive Ad Slot

Latest

latest

Python String 3 | Learn With Pirates

Wednesday, 21 April 2021

/ by Captain Jay's

Python String 3 | Learn With Pirates


Chapter 3: Python String

String is one data type in python

How to identify String

The string is a sequence of character enclose in quotes
p= "pirates"

How to Written String:

  • Single quotes
  • Double quotes
  • Triple quotes
s= 'pirates'     #Single quotes
p= "pirates"     #Double quotes
q= '''pirates''' #Triple quotes
print(s,p,q)

Output
pirates pirates pirates

String Slicing

Main String:
name = "Pirates" #0123456
A String in Python can be sliced for getting a part of the string.
Example For:
name = "Pirates" #0123456
print(name[0:3]) # return 0 to 3 character
print(name[1:3]) # return 1 to 3 character

Output
Pir ir

String index is started counting at 0 to (n-1) in python. 
How work of slice in the string:
slicing= name[int_start: int_end]

Negative indexing:
String index is started negative indexing at -1  to (n-1) in python.
name = "Pirates" #0123456
print(name[-5:-1]) #return same as [2:6]
print(name[2:6])
Output
rate rate

Slicing with skip value:
name = "Pirates" #0123456
print(name[0:6:4]) # return 0 and 4 index

Output
Pt

we can slice whatever we gain from a string.

Advance Slicing Techniques:
name = "Pirates" #0123456
print(name[:6]) # advance Slicing
print(name[0:]) # advance Slicing

Output
Pirate Pirates

Sting Functions:

Main String:
timeline = "Learn with pirates. Hey readers i hope you enjoy this tutorial"

len() function: 
This function returns a length of a string.
timeline = "Learn with pirates. Hey readers i hope you enjoy this tutorial"

print(len(timeline)) # length of timeline

Output
62

string.endwith(word)
this searches your word in the string if the word is present is give True, else False.
timeline = "Learn with pirates. Hey readers i hope you enjoy this tutorial"
print(timeline.endswith("rial")) # True Or False

Output
True

string.count("L")
this function return string total number of character of string.
timeline = "Learn with pirates. Hey readers i hope you enjoy this tutorial"
print(timeline.count("L")) # chracter count L in timeline

Output
1

string.capitalise()
return first character capital of string.
timeline = "Learn with pirates. Hey readers i hope you enjoy this tutorial"
print(timeline.capitalize()) # capitalize first word

Output
Learn with pirates. hey readers i hope you enjoy this tutorial

srinng.find(word)
This is work as a search option. it searches for a word a given answer.
timeline = "Learn with pirates. Hey readers i hope you enjoy this tutorial"
print(timeline.find("readers")) # which index

Output
24

string.replace(old_word,new_word)
this function is use for replace word in to string
timeline = "Learn with pirates. Hey readers i hope you enjoy this tutorial"
print(timeline.replace("Learn", "dO"))# return replace Learn with do

Output
dO with pirates. Hey readers i hope you enjoy this tutorial

Escape Sequence Characters

Identify
a sequence of characters after '\' backslash
Example:
# Escape Sequence Character
'''\n for new line
\t for Tab
\\ backslash
\' for singal quote
'''

Escape = "Pirates are Lead world\'s.\ngoogle\tis\n\\amazing"
print(Escape)

'''Output
Pirates are Lead world.
google is
\amazing'''

Termination

If you Like this tutorial please share with your friend and family. I make an amazing tutorial in this blog. If you have any queries and question ask me in the comment section below.

No comments

Post a Comment

Don't Miss
© all rights reserved
made with by templateszoo