Download our e-book of Introduction To Python
Balasaheb Garule
2 years ago
type(“Cats are cute”)
<class ‘str’>
s='Cats are cute' # python string example
print(s)
Cats are cute
s=" Cats are cute " # python string example
print(s)
Cats are cute
s= ‘Cats are cute” # python string example
SyntaxError: EOL while scanning string literal
s="Cats are "cute"" # python string example
SyntaxError: invalid syntax
s=’Cats are "cute"' # python string example
print(s)
Cats are “cute”
s="Cats are 'cute'" # python string example
print(s)
Cats are ‘cute’
s="'Cats' 'are' 'cute'" # python string example
print(s)
‘Cats’ ‘are’ ‘cute’
s="""Welcome
to
insideaiml""" # python string example
print(s)
Welcome
to
insideaiml
s="""Welcome\
to\
insideaiml""" # python string example
print(s)
Welcome to insideaiml
s="Cats"
s[0]="H"
Traceback (most recent call last):File “<pyshell#22>”, line 1, in <module>
s[0]=”H”
TypeError: ‘str’ object does not support item assignment
s="Cats are cute" # python string example
s
‘Cats are cute’
print(s)
Cats are cute
s="Cats are cute"
s[1]
‘a’
s="Cats are cute"
s[3:8]
‘s are’
s[:8]
‘Cats are’
s[9:]
‘ ut’
s[:]
‘Cats are cute’
s[:-2]
‘Cats are cu’
s[-2:]
‘te’
s[-3:-2]
‘u’
s[-3:-3]
”
s[3:3]
‘’
s = 'insideaiml'
reverse_str = s[::-1]
print(reverse_str)
lmiaedisni
# list of strings in python
names = ["amol", "rohan", "manisha"]
# print the list of strings
print(names)
[‘amol’, ‘rohan’, ‘manish’]
# loop over the names list, and print each string one at a time
for name in names:
print(name)
amol
rohan
manisha
x='Welcome to, '
y='insideaiml'
x+y # Python string concatenation
‘Welcome to insideaiml’
s='bye '
print(2*s)
bye bye
'40'+10
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
city=Pune
print("Age:",30,"City:",city)
Age: 30 City: Pune
name = ”insiadeaiml”
print(f"Welcome to {name}") # f string python
Welcome to insideaiml
str1 = "My name is {fname} and roll no is {rollno}".format(fname = "Rohan",rollno= 1)
str2 = "My name is {0} and roll no is {1} ".format( "Rohan", 1)
str3 = "My name is {} and roll no is {}".format( "Rohan", 1)
print(str1)
print(str2)
print(str3)
My name is Rohan and roll no is 1
My name is Rohan and roll no is 1
My name is Rohan and roll no is 1I love cats
Str1='Cats'
Str2 =’Dogs’
print("I love %s and %s" %(Str1,Str2))
I love Cats and Dogs
str1 = 'It\'s Amazing.'
print(str1)
str2 = "This will insert one \\ (backslash)."
print(str2)
str3 = "Hello\nWorld!"
print(str3)
str4 = "Hello\rWorld!"
print(str4)
str4 = "Hello\tWorld!"
print(str4)
#This example erases one character (backspace):
str5 = "Hello \bWorld!"
print(str5)
#A backslash followed by three integers will result in a octal value:
Str6 = "\151\156\163\151\144\145\141\151\155\154"
print(str6)
#A backslash followed by an 'x' and a hex number represents a hex value:
str7 ="\x69\x6e\x73\x69\x64\x65\x61\x69\x6d\x6c"
print(str7)
It's Amazing.
This will insert one \ (backslash).
Hello
World!
Hello
World!
Hello World!
Hello World!
insideaiml
insideaiml
s = “100”
print(type(s))
n = int(s)
print(type(n))
print(n)
<class 'str'>
<class 'int'>
100
s = “welcome to insideaiml”
list1 = s.split(“ “) #use delimiter as a space “ “
list1
['welcome', 'to', 'insideaiml'] #string to list in python
s = "3.14"
print(s)
print(type(s))
dec = float(s) #string to float in python
print(dec)
print(type(dec))
3.14
<class 'str'>
3.14
<class 'float'> #string to float in python
s='insideaiml'
len(s)
10
len(s[2:])
8
s= str(5+3j)
print(type(s))
print(s)
<class 'str'>
(5+3j)
s=”Python”
a.lower()
python
s.upper()
PYTHON
s=' Python '
s.strip()
‘Python’
s=”555”
s.isdigit()
True
s='pi3.14'
a.disdigit()
False
s=’python’
s.isalpha()
True
s='abc4'
s.isalpha()
False
s=' '
s.isspace()
True
s=' ab '
s.isspace()
False
s=”insideaiml”
s.startswith('in')
True
s=”python”
s.endswith('on')
True
# check if the string contains substring python
s=”insideaiml”
s.find(side)
2
s.find(on)
-1
“Mohan”.replace(‘Mo’, 'Ro')
‘Rohan’
'I, am, Student'.split(',')
[‘I’, ‘ am’, ‘ student’]
"+".join(['jan','feb','march'])
'jan+feb+march'