String

 # what is a data type

# data type is used to express the type of variable.

# str is a type used for text.
# int,float and complex are the types for number
# bool is a type for boolean values

# Type-Casting

# typecast integer to string

a = 10
b = str(a)
print(b)
print(type(b)) #we can check the type of variable

#typecast string to integer

c = "10"
d = int(c)
print(d)
print(type(d))

#String
#String is a type

str1 = "ChiTRAnsh "
str2 = "thakur"
str3 = str1+str2
print(str3)
print(len(str3))

#String is an array of characters.
print(str1[3]) #we can access the element using index number

#looping
for x in str2:
     print(x)

#slicing
print(str1[3:9])

#modify strings
print(str1)
print(str1.upper())
print(str1.lower())
print(str1.capitalize())

#strip() - strip() is used to remove the whitespaces from begining or end.
str5 = " chitransh thakur "
print(str5.strip())  

#split() - split() is used to split the string as desired.
str6 = "Hello , world"
print(str6.split(","))


#As we know that string and number can not be combined in Python.
#lets take an example

age1 = 21
sen1 = "My age is" + age1
print(sen1)
#TypeError: can only concatenate str (not "int") to str

#how can we resolve this....Using format()
Name = "chitransh thakur"
Class = "MCa"
age = 21

sen = "My Name is {0} , My age is {1} and My Class is {2}"
print(sen.format(Name.capitalize(),age,Class.capitalize()))

#Escape Sequences

print('Hello \n this \n is python \n a beautiful language') #new line

print("Hey Man! how you doing \"chitransh\"") # \"some text\" escape sequence

print("hello this is\b python a beautiful language")
#here \b means backspace ..it will delete 's' in the output






Comments