0% found this document useful (0 votes)
26 views6 pages

Day - 3

Uploaded by

akshayvarma13187
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views6 pages

Day - 3

Uploaded by

akshayvarma13187
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Day - 3

Random Number Module:


Python does not have a random() function to make a random number, but python has a
build in module called ‘random’ that can be used to make random numbers.

import random
print(random.randrange(1, 99))

STRINGS:-
1) Strings are arrays:
Strings in python are arrays of bytes representing unicode characters. However,
Python does not have a character data type, a single character is simply a string
with a length of 1.
Square brackets can be used to access elements of the string

2) *Looping through a string - will learn in For Loops


—-----------------------------
for x in “apple”:
print(x)
—-----------------------------

3) String Length:
To get the length of a string, we use the len() function.
—---------------------------------
a = “Hello World”
print(len(a))
—----------------------------------
**Please remember that space is itself a character when you’re printing the
length of a string.

4) Check String:
To check if a certain phrase or character is present in a string, we can use the
keyword ‘in’
—-------------------------------
Txt = “The best things in life are free”
print(“free” in Txt) #we get ‘true’ or ‘false’
#IT IS CASE SENSITIVE
—-------------------------------

*we’ll be learning in if…else statements


We can use the if statement to check as well
—---------------------------------------
txt=”the best things in life are free”
if “free” in txt:
print(“yes, free is present”)
—----------------------------------------

5) Slicing:
You can return a range of characters by using the slice syntax.
Specify the start index and the end index, separated by a colon, to return a part of
the string. #start index is the index of the first character that we want to print
#however the end index is of the character not included
—--------------------------------
txt = “hello world” #the first character is indexed as ‘0’
print(txt[0:5] #we are trying to print hello
—---------------------------------

6) Slicing to end:
By leaving out the end index, the range will go upto the end.
—---------------------------
txt = “hello world”
print(txt[6:]) #will print ‘world’
—----------------------------

7) Negative indexing:
We use negative indexing to slice from the end of the string.
In negative indexing we start from the index = -1.
—-----------------------------
txt = “hello world”
print(txt[-5:-1]) #this will print ‘worl’
—-----------------------------

Modifying Strings:-

1) Upper case:
the “upper()” method returns the string in upper case.
—-------------------------
txt = “hello world”
print(txt.upper()) #it returns ‘HELLO WORLD’
—--------------------------

2) Lower case:
the “lower()” method returns the string in lower case.
—-------------------------
txt = “HeLLo WorlD”
print(txt.lower()) #it returns ‘hello world’
—-------------------------

3) Remove whitespace:
Whitespace is the space before and/or after the actual text.
The “strip()” method removes any whitespace from the beginning or the end
—-----------------------------
txt = “ Hello, World! “
print(txt.strip()) #it returns ‘Hello, World!’
—------------------------------

4) Replace strings:
The replace() method replaces a string with another string.
—-----------------------------------
txt = “Hello World” # it is case sensitive
print(txt.replace(“World”,”Akshay”)) #returns ‘Hello Akshay’
—-----------------------------------

5) ***Split string: (ill be telling when we get to lists)


The “split()” method returns a list where the text between the specified separator
becomes the list items.
The split() method splits the string into substrings if it finds instances of the
separator.
—--------------------------------------
txt = “hello, world”
print(txt.split(“,”)) #returns [‘hello’, ‘world’]
—-------------------------------------

6) String Concatenation:
To concatenate, or combine, two strings you can use the ‘+’ operator
—---------------------------
a= “hello”
b= “world”
print(a+b) #returns ‘helloworld’
—---------------------------
To add a space between them, add a “ “
—----------------------------
a= “hello”
b=”world”
c= a+ “ “ +b
print(c) #returns “hello world”
—----------------------------

Format Strings:-
We can combine strings and numbers by using f-strings or the format() method.

1) Format Method:
—---------------------------------------
age= 17
txt= “My name is Akshay, and i am {}”
print(txt.format(age)) #returns “my name is akshay, and i am 17”
—---------------------------------------

2) F- string:
To specify as an f-string, simply put an ‘f’ in front of the string literal, and add
curly brackets {} placeholders for variables and other operations.
—----------------------------------------

—----------------------------------------

You might also like