#آموزش
🔴 string formating
قسمت اول
# old string formating style
print("My name is %s %s" %(name, family))
output : My name is hasan abshenasan
print("My name is %(name)s %(family)s" %{'name' : name, 'family' : family})
output : My name is hasan abshenasan
# new string formating style introduced in python 3 but later back-ported to Python 2.7
print("My name is {name} {family}".format(name = name.title(), family = family))
output : My name is hasan abshenasan
# String Interpolation / f-Strings (Python 3.6+)
print(f"My name is {name.title()} {family}")
output : My name is Hasan abshenasan
num1 = 5
num2 = 4
print(f"Five plus four is {num1 + num2} not {2 * (num1 + num2)}.")
output : Five plus four is 9 not 18
# Template Strings (Standard Library)
from string import Template
temp = Template('Hey, $name!')
print(temp.substitute(name=name))
output : Hey, hasan
template_String = "My name is $name $family"
print(Template(template_String).substitute(name = name, family = family))
output : My name is hasan abshenasan
txt = "hello world"
print(txt[:6])
output :hello
print(txt[6:])
output :world
print(txt[1:6])
output : hello
print(txt[::-1])
output : dlrow olleh
print(txt.capitalize())
output : Hello world
print(txt.title())
output : Hello World
print(txt.count('l'))
output : 3
print(txt.count('l',6,10))
output : 1
print(txt.endswith('world'))
output : True
print(txt.endswith('world', 5, 11))
output : True
print(txt.startswith("hello"))
output : True
print(txt.startswith("hello", 0, 8))
output : True
txt = "this is\ttesting example"
print(txt.expandtabs(16))
output : this is testing example
print(txt.find('exam'))
output : 16
print(txt.find('exam', 0, 20))
output : 16
print(txt.index('exam'))
output : 16
#difrent betwen index and find is if index cant find give error but find not
txt = "tabriz2018"
print(txt.isalnum())
#return true if text is alphabetic or numeric or both
output : True
txt = "tabriz"
print(txt.isalpha())
#return true if text only be alphabetic
output : True
num = "455635"
print(num.isdigit())
# return true if text consists of digits only
output : True
txt = "tabriz"
print(txt.isspace())
output : True
print(txt.isupper())
output : False
txt = " "
print(txt.isspace())
output : True
txt = "Tabriz City"
print(txt.istitle())
output : True
txt = "Tabriz city"
print(txt.istitle())
output : False
print(len(txt))
output : 11
txt = "TABRIZ"
print(txt.lower())
output : tabzriz
txt = "tabriz"
print(txt.upper())
ouput : TABRIZ
txt = "****tabriz****"
print(txt.rstrip('*'))
output :****tabriz
print(txt.lstrip('*'))
output : tabriz****
print(txt.strip('*'))
output : tabriz
intab = 'aeiou'
outtab = '12345'
txt = "this is test example"
transtab = txt.maketrans(intab, outtab)
print(txt.translate(transtab))
output : th3s 3s t2st 2x1mpl2
txt = "this is test example"
print(max(txt))
output : z
txt = "fkdgn"
print(min(txt))
ouput : d
txt = "this is test example"
print(min(txt.replace(' ','')))
ouput : a
با تشکر از مهندس
@milad_ghasemi_1
❇️❇️ @raspberry_python
🔴 string formating
قسمت اول
# old string formating style
print("My name is %s %s" %(name, family))
output : My name is hasan abshenasan
print("My name is %(name)s %(family)s" %{'name' : name, 'family' : family})
output : My name is hasan abshenasan
# new string formating style introduced in python 3 but later back-ported to Python 2.7
print("My name is {name} {family}".format(name = name.title(), family = family))
output : My name is hasan abshenasan
# String Interpolation / f-Strings (Python 3.6+)
print(f"My name is {name.title()} {family}")
output : My name is Hasan abshenasan
num1 = 5
num2 = 4
print(f"Five plus four is {num1 + num2} not {2 * (num1 + num2)}.")
output : Five plus four is 9 not 18
# Template Strings (Standard Library)
from string import Template
temp = Template('Hey, $name!')
print(temp.substitute(name=name))
output : Hey, hasan
template_String = "My name is $name $family"
print(Template(template_String).substitute(name = name, family = family))
output : My name is hasan abshenasan
txt = "hello world"
print(txt[:6])
output :hello
print(txt[6:])
output :world
print(txt[1:6])
output : hello
print(txt[::-1])
output : dlrow olleh
print(txt.capitalize())
output : Hello world
print(txt.title())
output : Hello World
print(txt.count('l'))
output : 3
print(txt.count('l',6,10))
output : 1
print(txt.endswith('world'))
output : True
print(txt.endswith('world', 5, 11))
output : True
print(txt.startswith("hello"))
output : True
print(txt.startswith("hello", 0, 8))
output : True
txt = "this is\ttesting example"
print(txt.expandtabs(16))
output : this is testing example
print(txt.find('exam'))
output : 16
print(txt.find('exam', 0, 20))
output : 16
print(txt.index('exam'))
output : 16
#difrent betwen index and find is if index cant find give error but find not
txt = "tabriz2018"
print(txt.isalnum())
#return true if text is alphabetic or numeric or both
output : True
txt = "tabriz"
print(txt.isalpha())
#return true if text only be alphabetic
output : True
num = "455635"
print(num.isdigit())
# return true if text consists of digits only
output : True
txt = "tabriz"
print(txt.isspace())
output : True
print(txt.isupper())
output : False
txt = " "
print(txt.isspace())
output : True
txt = "Tabriz City"
print(txt.istitle())
output : True
txt = "Tabriz city"
print(txt.istitle())
output : False
print(len(txt))
output : 11
txt = "TABRIZ"
print(txt.lower())
output : tabzriz
txt = "tabriz"
print(txt.upper())
ouput : TABRIZ
txt = "****tabriz****"
print(txt.rstrip('*'))
output :****tabriz
print(txt.lstrip('*'))
output : tabriz****
print(txt.strip('*'))
output : tabriz
intab = 'aeiou'
outtab = '12345'
txt = "this is test example"
transtab = txt.maketrans(intab, outtab)
print(txt.translate(transtab))
output : th3s 3s t2st 2x1mpl2
txt = "this is test example"
print(max(txt))
output : z
txt = "fkdgn"
print(min(txt))
ouput : d
txt = "this is test example"
print(min(txt.replace(' ','')))
ouput : a
با تشکر از مهندس
@milad_ghasemi_1
❇️❇️ @raspberry_python