Day - 3
Day - 3
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
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
—-------------------------------
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’
—-----------------------------------
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.
—----------------------------------------
—----------------------------------------