Find Frequency of Characters in Python
plore various methods to count the frequency of characters in a given string. One simple method to
count frequency of each character is by using a dictionary.
Using Dictionary
The idea is to traverse through each character in the string and keep a count of how many times it
appears.
Python
s = "GeeksforGeeks"
freq = {}
for c in s:
if c in freq:
freq[c] += 1
else:
freq[c] = 1
print(freq)
Output
{'G': 2, 'e': 4, 'k': 2, 's': 2, 'f': 1, 'o': 1, 'r': 1}
Using Counter from collections Library
The collections module has a built-in Counter class to count character frequencies in a string.
Python
1
from collections import Counter
2
3
s = "GeeksforGeeks"
4
5
# Count character frequency using Counter
6
freq = Counter(s)
7
8
print(dict(freq))
example 2 -:
list1 = ['I am ', 'You are ']
list2 = ['healthy', 'fine', 'geek']
# Store length of list2 in list2_size
list2_size = len(list2)
# Running outer for loop to
# iterate through a list1.
for item in list1:
# Printing outside inner loop
print("start outer for loop ")
# Initialize counter i with 0
i=0
# Running inner While loop to
# iterate through a list2.
while(i < list2_size):
# Printing inside inner loop
print(item, list2[i])
# Incrementing the value of i
i = i+1
# Printing outside inner loop
print("end for loop ")
Number Patterns Programs In Python
I have created various programs that print different styles of number patterns. Let’s see them one by one. Let’s
print the following number pattern using a for loop.
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Program:
rows = 6
# if you want user to enter a number, uncomment the below line
# rows = int(input('Enter the number of rows'))
# outer loop
for i in range(rows):
# nested loop
for j in range(i):
# display number
print(i, end=' ')
# new line after each row
print('')
Run
In this number pattern, we display a single digit on the first row, two digits on the second row, and three digits
on the third row. This process will repeat until the number of rows is reached.
Note:
The count of numbers on each row is equal to the current row number.
Also, each number is separated by space.
We used a nested loop to print the pattern
Pyramid pattern of numbers
Let’s see how to print the following half-pyramid pattern of numbers
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Note: In each row, every next number is incremented by 1.
Program:
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end=' ')
print('')
Run
Inverted pyramid pattern of numbers
An inverted pyramid is a downward pattern where numbers get reduced in each iteration, and on the last row, it
shows only one number. Use reverse for loop to print this pattern.
Pattern
1 1 1 1 1
2 2 2 2
3 3 3
4 4
Program
rows = 5
b=0
# reverse for loop from 5 to 0
for i in range(rows, 0, -1):
b += 1
for j in range(1, i + 1):
print(b, end=' ')
print('\r')
Run
Inverted Pyramid pattern with the same digit
Pattern: –
5 5 5 5 5
5 5 5 5
5 5 5
5 5
Program: –
rows = 5
num = rows
# reverse for loop
for i in range(rows, 0, -1):
for j in range(0, i):
print(num, end=' ')
print("\r")
Run
Another inverted half-pyramid pattern with a number
Pattern: –
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
Program
rows = 5
for i in range(rows, 0, -1):
for j in range(0, i + 1):
print(j, end=' ')
print("\r")
Run
Alternate numbers pattern using a while loop
Let’s see how to use the while loop to print the number pattern.
Pattern: –
1
3 3
5 5 5
7 7 7 7
9 9 9 9 9
Program: –
rows = 5
i=1
while i <= rows:
j=1
while j <= i:
print((i * 2 - 1), end=" ")
j=j+1
i=i+1
print('')
Run
Reverse number pattern
Let’s see how to display the pattern of descending order of numbers
Pattern 1: –
5 5 5 5 5
4 4 4 4
3 3 3
2 2
This pattern is also called as a inverted pyramid of descending numbers.
Program: –
rows = 5
# reverse loop
for i in range(rows, 0, -1):
num = i
for j in range(0, i):
print(num, end=' ')
print("\r")
Run
Reverse Pyramid of Numbers
Pattern 2: –
2 1
3 2 1
4 3 2 1
5 4 3 2 1
Note: It is a downward increment pattern where numbers get increased in each iteration. At each row, the
amount of number is equal to the current row number.
Program
rows = 6
for i in range(1, rows):
for j in range(i, 0, -1):
print(j, end=' ')
print("")
Run
Another reverse number pattern
Pattern: –
5 4 3 2 1
4 3 2 1
3 2 1
2 1
Program: –
rows = 5
for i in range(0, rows + 1):
for j in range(rows - i, 0, -1):
print(j, end=' ')
print()
Run
Print reverse number from 10 to 1
Pattern: –
1
3 2
6 5 4
10 9 8 7
Program: –
start = 1
stop = 2
current_num = stop
for row in range(2, 6):
for col in range(start, stop):
current_num -= 1
print(current_num, end=' ')
print("")
start = stop
stop += row
current_num = stop
Run
Number triangle pattern
Let’s see how to print the right-angled triangle pattern of numbers
Pattern: –
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Program: –
rows = 6
for i in range(1, rows):
num = 1
for j in range(rows, 0, -1):
if j > i:
print(" ", end=' ')
else:
print(num, end=' ')
num += 1
print("")
Run
Pascal’s triangle pattern using numbers
To build the pascal triangle, start with “1” at the top, then continue placing numbers below it in a triangular
pattern.
Each number is the numbers directly above it added together.
Pattern:
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
Program: –
def print_pascal_triangle(size):
for i in range(0, size):
for j in range(0, i + 1):
print(decide_number(i, j), end=" ")
print()
def decide_number(n, k):
num = 1
if k > n - k:
k=n-k
for i in range(0, k):
num = num * (n - i)
num = num // (i + 1)
return num
# set rows
rows = 7
print_pascal_triangle(rows)
Run
Square pattern with numbers
Pattern: –
1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5
5 5 5 5 5
Program: –
rows = 5
for i in range(1, rows + 1):
for j in range(1, rows + 1):
if j <= i:
print(i, end=' ')
else:
print(j, end=' ')
print()
Run
Multiplication table pattern
Pattern: –
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
Program: –
rows = 8
# rows = int(input("Enter the number of rows "))
for i in range(1, rows + 1):
for j in range(1, i + 1):
# multiplication current column and row
square = i * j
print(i * j, end=' ')
print()
Run
Pyramid pattern of stars in python
This section will see how to print pyramid and Star (asterisk) patterns in Python. Here we will print the
following pyramid pattern with Star (asterisk).
Half pyramid pattern with stars(*)
Full pyramid pattern with stars
Inverted pyramid Pattern with stars
Triangle pattern with stars
Right-angled triangle pattern with stars
Simple half pyramid pattern: –
* *
* * *
* * * *
* * * * *
This pattern is also known as a right angle triangle pyramid.
Program: –
# number of rows
rows = 5
for i in range(0, rows):
# nested loop for each column
for j in range(0, i + 1):
# print star
print("*", end=' ')
# new line after each row
print("\r")
Run
Right triangle pyramid of Stars
Pattern: –
* *
* * *
* * * *
* * * * *
This pattern is also called as mirrored right triangle
Program: –
# number of rows
rows = 5
k = 2 * rows - 2
for i in range(0, rows):
# process each column
for j in range(0, k):
# print space in pyramid
print(end=" ")
k=k-2
for j in range(0, i + 1):
# display star
print("* ", end="")
print("")
Run
Alternative Solution:
rows = 5
for j in range(1, rows+1):
print("* " * j)
Run
Downward half-Pyramid Pattern of Star
Pattern: –
* * * * *
* * * *
* * *
* *
Note: We need to use the reverse nested loop to print the downward pyramid pattern of stars
Program: –
rows = 5
for i in range(rows + 1, 0, -1):
# nested reverse loop
for j in range(0, i - 1):
# display star
print("*", end=' ')
print(" ")
Run
Downward full Pyramid Pattern of star
Let’s see how to print reversed pyramid pattern in Python.
Pattern: –
* * * * * *
* * * * *
* * * *
* * *
* *
Program:
rows = 5
k = 2 * rows - 2
for i in range(rows, -1, -1):
for j in range(k, 0, -1):
print(end=" ")
k=k+1
for j in range(0, i + 1):
print("*", end=" ")
print("")
Run
Right down mirror star Pattern
Pattern: –
*****
****
***
**
In this pattern, we need to use two nested while loops.
Program: –
rows = 5
i = rows
while i >= 1:
j = rows
while j > i:
# display space
print(' ', end=' ')
j -= 1
k=1
while k <= i:
print('*', end=' ')
k += 1
print()
i -= 1
Run
Equilateral triangle pattern of star
Pattern: –
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
Program: –
print("Print equilateral triangle Pyramid using asterisk symbol ")
# printing full Triangle pyramid using stars
size = 7
m = (2 * size) - 2
for i in range(0, size):
for j in range(0, m):
print(end=" ")
# decrementing m after each loop
m=m-1
for j in range(0, i + 1):
print("* ", end=' ')
print(" ")
Run
Print two pyramids of stars
Pattern: –
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
Program: –
rows = 6
for i in range(0, rows):
for j in range(0, i + 1):
print("*", end=' ')
print(" ")
print(" ")
for i in range(rows + 1, 0, -1):
for j in range(0, i - 1):
print("*", end=' ')
print(" ")
Run
Right start pattern of star
Pattern: –
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
We also call this pattern as a right pascal’s triangle.
Program: –
rows = 5
for i in range(0, rows):
for j in range(0, i + 1):
print("*", end=' ')
print("\r")
for i in range(rows, 0, -1):
for j in range(0, i - 1):
print("*", end=' ')
print("\r")
Run
Left triangle pascal’s pattern
Pattern: –
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
Program: –
rows = 5
i=1
while i <= rows:
j=i
while j < rows:
# display space
print(' ', end=' ')
j += 1
k=1
while k <= i:
print('*', end=' ')
k += 1
print()
i += 1
i = rows
while i >= 1:
j=i
while j <= rows:
print(' ', end=' ')
j += 1
k=1
while k < i:
print('*', end=' ')
k += 1
print('')
i -= 1
Run
Sandglass pattern of star
Pattern: –
* * * * *
* * * *
* * *
* *
* *
* * *
* * * *
* * * * *
To print this pattern we need to use two set of three while loops.
Program: –
rows = 5
i=0
while i <= rows - 1:
j=0
while j < i:
# display space
print('', end=' ')
j += 1
k=i
while k <= rows - 1:
print('*', end=' ')
k += 1
print()
i += 1
i = rows - 1
while i >= 0:
j=0
while j < i:
print('', end=' ')
j += 1
k=i
while k <= rows - 1:
print('*', end=' ')
k += 1
print('')
i -= 1
Run
Pant style pattern of stars
Pattern: –
****************
*******__*******
******____******
*****______*****
****________****
***__________***
**____________**
*______________*
Program: –
rows = 14
print("*" * rows, end="\n")
i = (rows // 2) - 1
j=2
while i != 0:
while j <= (rows - 2):
print("*" * i, end="")
print("_" * j, end="")
print("*" * i, end="\n")
i=i-1
j=j+2
Run
Diamond-shaped pattern of stars
Pattern: –
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
Program: –
rows = 5
k = 2 * rows - 2
for i in range(0, rows):
for j in range(0, k):
print(end=" ")
k=k-1
for j in range(0, i + 1):
print("* ", end="")
print("")
k = rows - 2
for i in range(rows, -1, -1):
for j in range(k, 0, -1):
print(end=" ")
k=k+1
for j in range(0, i + 1):
print("* ", end="")
print("")
Run
Another diamond pattern of star
Pattern: –
* *
* *
* *
* *
* *
* *
* *
*
Program: –
rows = 5
i=1
while i <= rows:
j = rows
while j > i:
# display space
print(' ', end=' ')
j -= 1
print('*', end=' ')
k=1
while k < 2 * (i - 1):
print(' ', end=' ')
k += 1
if i == 1:
print()
else:
print('*')
i += 1
i = rows - 1
while i >= 1:
j = rows
while j > i:
print(' ', end=' ')
j -= 1
print('*', end=' ')
k=1
while k <= 2 * (i - 1):
print(' ', end=' ')
k += 1
if i == 1:
print()
else:
print('*')
i -= 1
Run
Alphabets and letters pattern
In Python, there are ASCII values for each letter. To print the patterns of letters and alphabets, we need to
convert them to their ASCII values.
Decide the number of rows
Start with ASCII number 65 ( ‘A’)
Iterate a loop and in nested for loop use the char function to convert ASCII number to its
equivalent letter.
Let’ see now how to print alphabets and letters patterns in Python.
Pattern: –
B C
D E F
G H I J
K L M N O
P Q R S T U
V W X Y Z [ \
This pattern is knows as right-angled pattern with characters.
Program: –
# ASCII number of 'A'
ascii_number = 65
rows = 7
for i in range(0, rows):
for j in range(0, i + 1):
character = chr(ascii_number)
print(character, end=' ')
ascii_number += 1
print(" ")
Run
Pattern to display letter of the word
Let’s see how to print word ‘Python’ in Pattern: –
Py
Pyt
Pyth
Pytho
Python
Program: –
word = "Python"
x = ""
for i in word:
x += i
print(x)
Run
Equilateral triangle pattern of characters/alphabets
Pattern: –
A
B C
D E F
G H I J
K L M N O
P Q R S T U
V W X Y Z [ \
Program: –
print("Print equilateral triangle Pyramid with characters ")
size = 7
asciiNumber = 65
m = (2 * size) - 2
for i in range(0, size):
for j in range(0, m):
print(end=" ")
m=m-1
for j in range(0, i + 1):
character = chr(asciiNumber)
print(character, end=' ')
asciiNumber += 1
print(" ")
Run
Pattern of same character
Pattern: –
V V
V V V
V V V V
V V V V V
Program: –
# Same character pattern
character = 'V'
# convert char to ASCII
char_ascii_no = ord(character)
for i in range(0, 5):
for j in range(0, i + 1):
# Convert the ASCII value to the character
user_char = chr(char_ascii_no)
print(user_char, end=' ')
print()
Run
Let’s see some more miscellaneous patterns
More miscellaneous Patterns
Pyramid of horizontal number tables
Pattern: –
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
10 20 30 40 50 60 70 80 90 100
Program: –
# Pyramid of horizontal tables of numbers
rows = 10
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(i * j, end=' ')
print()
Run
Double the number pattern
Pattern: –
2 1
4 2 1
8 4 2 1
16 8 4 2 1
32 16 8 4 2 1
64 32 16 8 4 2 1
128 64 32 16 8 4 2 1
Note: In each column, every number is double it’s the preceding number.
Program: –
rows = 9
for i in range(1, rows):
for j in range(-1 + i, -1, -1):
print(format(2 ** j, "4d"), end=' ')
print("")
Run
Random number pattern
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
Program: –
rows = 9
for i in range(1, rows):
for i in range(0, i, 1):
print(format(2 ** i, "4d"), end=' ')
for i in range(-1 + i, -1, -1):
print(format(2 ** i, "4d"), end=' ')
print("")
Run
Pyramid of numbers less than 10
Pattern: –
1
2 3 4
5 6 7 8 9
Program: –
current_num = 1
stop = 2
rows = 3
for i in range(rows):
for column in range(1, stop):
print(current_num, end=' ')
current_num += 1
print("")
stop += 2
Run
Pyramid of numbers up to 10
Pattern: –
2 3
4 5 6
7 8 9 10
Program: –
current_num = 1
rows = 4
stop = 2
for i in range(rows):
for column in range(1, stop):
print(current_num, end=' ')
current_num += 1
print("")
stop += 1
Run
Even number pattern
Pattern: –
10
10 8
10 8 6
10 8 6 4
10 8 6 4 2
Programs: –
rows = 5
last_num = 2 * rows
even_num = last_num
for i in range(1, rows + 1):
even_num = last_num
for j in range(i):
print(even_num, end=' ')
even_num -= 2
print("\r")
Run
Unique pyramid pattern of digits
Pattern: –
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
Program: –
rows = 6
for i in range(1, rows + 1):
for j in range(1, i - 1):
print(j, end=" ")
for j in range(i - 1, 0, -1):
print(j, end=" ")
print()
Run
Pattern double number on each column
Pattern: –
0 1
0 2 4
0 3 6 9
0 4 8 12 16
0 5 10 15 20 25
0 6 12 18 24 30 36
Program: –
rows = 7
for i in range(0, rows):
for j in range(0, i + 1):
print(i * j, end=' ')
print()
Run
Number reduction pattern
Pattern: –
1 2 3 4 5
2 3 4 5
3 4 5
4 5
Program: –
rows = 5
for i in range(0, rows + 1, 1):
for j in range(i + 1, rows + 1, 1):
print(j, end=' ')
print()
Run
Pant style pattern of numbers
Pattern: –
5 4 3 2 1 1 2 3 4 5
5 4 3 2 2 3 4 5
5 4 3 3 4 5
5 4 4 5
5 5
Program: –
rows = 6
for i in range(0, rows):
for j in range(rows - 1, i, -1):
print(j, '', end='')
for l in range(i):
print(' ', end='')
for k in range(i + 1, rows):
print(k, '', end='')
print('\n')
Run
Pattern with a combination of numbers and stars
Pattern: –
1 * 2 * 3 * 4
1 * 2 * 3
1 * 2
Program: –
row = 4
for i in range(0, row):
c=1
print(c, end=' ')
for j in range(row - i - 1, 0, -1):
print('*', end=' ')
c=c+1
print(c, end=' ')
print('\n')
.
Practice Problem
Pattern: –
2 4
4 8 8
8 16 16 16
Solution: –
num = 4
counter = 0
for x in range(0, num):
for y in range(0, x + 1):
print(counter, end=" ")
counter = 2 ** (x + 1)
print()
Q. Accept the number of days from the user and calculate the charge of library according to the
following criteria. Till five days it is ₹2 per day, 6 to 10 days it is ₹3 per day, 11 to 15 days it is ₹4.00
per day and after 15 days it is five Rupees per day.
x=n=int(input("Enter number of days: "))
sum=0
if n>15:
sum=sum+(n-15)*5
n=n-(n-15)
if 15>=n>=11:
sum=sum+(n-10)*4
n=n-(n-10)
if 10>=n>=6:
sum=sum+(n-5)*3
n=n-(n-5)
if 5>=n:
sum=sum+n*2
print("Amount after",x,"days is",sum)
Output
Enter number of days: 15
Amount after 15 days is 45
Q. Accept the kilometers covered and calculate the bill according to the following criteria. For 1st 10
kilometers it is ₹11.00 per kilometer, For next 90 kilometers it is rupees 10 per kilometer and after
that it is ₹9 per kilometer.
Ans. k=int(input("Enter number of days: "))
bill=0
if k>90:
bill=bill+(k-90)*9
k=k-(k-90)
if k>=11:
bill= bill+(k-10)*10
k=k-(k-10)
if k<=10:
bill=bill+ k*11
print("kilo meter",k,"bill",bill)
Q. Write a program to calculate the electricity [Link] to the following criteria, for 1st 100 units
there is no charge, For next 100 units there is ₹5 per unit and after 200 units, the price is rupees 10
per [Link] the input is 350 then total bill amount is Rupees 2000.