UNIT - 3_Python
UNIT - 3_Python
Declaration of Strings
mystring = "This is not my first String"
print (mystring)
Output : This is not my first String
print("Returned letter",word[i])
i += 1
18-11-2024 Ankita Sharma Unit 3 10
3.
n = int(input("Enter an integer: "))
i=1
while i <= 10:
mul = i*n
i += 1
print(mul)
while n!=0:
r = n%10
rev = rev * 10 + r
n = n//10
print("Reversed number:",rev)
Example 1:
str = "HELLO"
str[0] = "h"
print(str)
Example 2 :
str = "HELLO"
print(str)
str = "hello"
print(str)
Example :
str1 = "JAVATPOINT"
del str1
print(str1)
Assume a as Hello.
x = "Good"
y = "Work"
print(x + y) # Output: GoodWork
To add a space between words, we can include a space in one of the
strings or add a space string:
age = 25
message = "I am " + str(age) + " years old."
18-11-2024 Ankita Sharma Unit 3 20
print(message) # Output: I am 25 years old.
String Repetition
• It allow you to repeat a string by certain number
• We can repeat a string multiple times using the * operator:
For example, to get the first three items of a list named L, you
would use L[:3].
For example, to obtain the last three items, you’d use L[6:].
Example 1
x = ["apple", "banana", "cherry"] and y = x.
In this example, x is y will return True because x and y are the same
object.
Creating a Tuple
A tuple is formed by enclosing all of the items (elements)
in parentheses () rather than square brackets [], and each element is
separated by commas.Tuple can also be created without parantheses.
You can also specify nested tuples, which have one or more entries
that are lists, tuples, or dictionaries.
your_idade = 25
if_you_are_working = True
string = 'Coding'
string = string.replace('ing', 'ers')
print(string)
Output: Coders
string.split(separator, maxsplit)
myList = string.split(“ “)
Print(myList)
Output:
[‘welcome’,’to’,coding’,’line’]
print(index)
Output : 8
String=“ ,”.join(string)
Print(string)
Output :
W,e,l,c,o,m,e, ,t,o, ,c,o,d,i,n,g, ,l,i,n,e,s
copy():
The statements after the return statements are not executed. If the
return statement is without any expression, then the special value
None is returned.
while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1
return lcm
num1 = 54
num2 = 24
Links : https://www.programiz.com/python-
programming/examples