#We can iterate through the strings using a for loop.
name = 'syam'
for char in name:
print(char)
#The iteration is case sensitive.
lang = 'MaLAyalAM'
for char in lang:
if char == 'a':
print(char)
#We can check the occurance of a letter(both lower case and upper case) using below code.
lang = 'MaLAyalAM'
for char in lang:
if char == 'a' or char == 'A':
print(char)
#If we need to check the count. Here as well, its case sentisitive.
lang.count('a')
#To get the exact count
lang.lower().count('a')
#Using count increment.
lang = 'MaLAyaLAM'
count = 0
for char in lang:
if char.lower() == 'a':
count = count+1
print(count)
"Hi {} You are {} years old".format("alex","40")
lang = 'MaLAyaLAM'
count = 0
for char in lang:
if char.lower() == 'a':
count = count+1
print("Total number of A is : {}".format(count))
#A list can be intiated using below
name = []
type(name)
#To add an entry to a list.
#append()
name.append("syam")
name
#A list can have entries with different datatypes.
name.append("mi")
name.append("oneplus")
name.append("samsung")
name.append(1234)
name.append(127.898)
name
#Length of a list can be calculated using len() function.
len(name)
#Indexing works in list.
name[0]
#An item in the list can also be iterated.
name[0][1]
#However it is not possible for an integer.
name[4][1]
name.append("syam")
name
#We can find the count of any entry in a list using count function.
name.count('syam')
msg = "Amazon Web Services uses information from your Amazon.com account to identify you and allow access to Amazon Web Services."
#Contents of the string will be converted to a list by split() method.
#By default split() method splits with space as delimeter.
msg.split()
#We can give a delimeter in split function.
"01/12/2019".split("/")
msg.count("Amazon")
msg.lower().split().count('amazon') #Here the correct occurance will only be taken.
words = msg.lower().split() #Creating a list of words from the string.
for word in words: #looping through the list
if word.startswith("a"): #To find all the words in list which starts with letter 'a'
print(word)
#Fetching the words starting with 'a' and adding them to a list.
result = []
words = msg.lower().split()
for word in words:
if word.startswith("a"):
result.append(word)
print(result)
football = ["messi" , "vijayan" , "ronaldo" , "suarez"]
'messi' in football
'syam' not in football
#We can re write the same code to not include duplicate entries.
result = []
words = msg.lower().split()
for word in words:
if word.startswith("a"):
if word not in result:
result.append(word)
print(result)
if 1 == 1:
print("OK")
if "syam":
print("OK")
else:
print("NO")
bool('syam') #Boolean of any value is true(except 0).
bool(1)
bool(0)
bool(None)
bool('')
bool(' ')
if True:
print("OK")
else:
print("NO")
import os #To import a module
os.listdir("/etc") #This will print all files/folders inside the given file/folder. Like ls command.
#endswith() can be used to check if a string ends with a specific value.
for item in os.listdir("/etc"):
if item.endswith(".conf"):
print(item)
import os
os.path.join("syam" , "test") #This is used to create file locations names in Python.
os.path.join("/etc" , "httpd" , "conf" , "httpd.conf")
#To check for every conf file inside /etc and print its absolute path.
for item in os.listdir("/etc"):
abs_path = os.path.join("/etc" , item)
if abs_path.endswith(".conf"):
print(abs_path)
#To find all conf files inside /etc and print it's size.
for item in os.listdir("/etc"):
abs_path = os.path.join("/etc", item)
if abs_path.endswith(".conf"):
size = os.path.getsize(abs_path)
print("{} : {}".format(item,size))