Variables & String in Python

Variables & String in Python

Variables are containers for storing data values.
Unlike other programming languages, Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.


var1 = "456"
print(type(var1))
var2 = 69.6
print(str(var2) + var1)

var3 = 48
print(var3 + float(var2))
print("Hello World\n" * 1000)   


''' Python Escape Sequence '''

print("akash \v kumar \v singh")               # \v used to print a rectangular box
print("akash \v \n kumar singh ")             # \n used to print the statement in Next Line
print("Hello \t World")                               # \t used for tab(four times space)


'''  Program : Addition Calculator  '''

num1 = int(input("Enter First Number : "))
num2 = int(input("Enter Second Number : "))
Tsum = num1 + num2
print("Sum of",num1 ,"and", num2, "is ->", Tsum)



String in Python

In Python, String is arrays of bytes representing Unicode characters.
The element of string can be accessed by square brackets known as String Slicing.

mystr = "Akash is a good boy"

print(mystr.endswith("oy"))             # Return TRUE if it's ends with oy
print(mystr.count("o"))                  # Count() tell that how many time the letter was present in str
print(mystr.capitalize())               # Capitalize used to capitalize first letter of String

print(mystr.upper())                      # Upper used to Capitalize whole String
print(mystr.find("is"))                       # find() used to know index of specific element 
print(mystr.replace("Akash", "Tony"))           # Akash is replaced by Tony
print(mystr.casefold())                             # lower whole String

l = mystr.split()            # It will convert in LIST
print(l)
print(type(l))

txt = " Hello World "
x =  txt.strip()                    # Return the string without any whitespace at the beginning or the end.
txt = txt.replace("H", "J")         # Replace the character H with a J.

age = 3
txte = "My name is John, and I am {}"  
print(txte.format(age))               # The age you input in Program it will print that





Disqus Comments