0% found this document useful (0 votes)
2 views4 pages

Python Patterns and String Manipulations

Uploaded by

manik1062000
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Python Patterns and String Manipulations

Uploaded by

manik1062000
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Square pattern printing: *****

*****
rows = 5 *****
for i in range(rows): *****
for j in range(rows): *****
print("*", end="")

print() # Move to the next line after each row

right angled triangle left aligned

rows = 5 **

for i in range(1, rows + 1): ***

for j in range(i): ****

print("*", end="") *****

print()

right angled triangle right aligned

**
rows = 5
***
for i in range(1, rows + 1):
****
print(" " * (rows - i) + "*" * i)
*****

full triangle print:


*
rows = 5
***
for i in range(rows):
*****
print(" " * (rows - i - 1) + "*" * (2 * i + 1))
*******

*********

inverted triangle:
*********
rows = 5
*******
for i in range(rows, 0, -1):
*****
print(" " * (rows - i) + "*" * (2 * i - 1))
***

number pattern:
1
rows = 5
12
for i in range(1, rows + 1):
123
for j in range(1, i + 1):
1234
print(j, end="")
12345
print()

string reversal:

original_string = "Hello"

reversed_string = original_string[::-1]

print(reversed_string)
character counting in a string:
{'B': 1, 'r': 3, 'a': 2, 'i': 3, 'n': 2, 'w': 1, 'e': 2, ' ': 1, 'U': 1,
s = "brainware university" 'v': 1, 's': 1, 't': 1, 'y': 1}
freq = {}

for c in s:

if c in freq:

freq[c] += 1

else:

freq[c] = 1

print(freq)

removing vowel in a string:

def rem_vowel(string):

vowels = ['a','e','i','o','u']

result = [letter for letter in string if [Link]() not in vowels]

result = ' '.join(result)

print(result)

# Driver program

string = "brainware university"

rem_vowel(string)

string = "barasat"

rem_vowel(string)

Removing duplicates

def duplicates(aa):

res = []

# Iterate through each value in the list 'aa'

for val in aa:

# Check if the value is not already in 'res'


if val not in res:

# If not present, append it to 'res' [1, 2, 3, 4, 5]

[Link](val) ['a', 'b', 'c']

return res # Return the result

# Test cases

aa = [1, 2, 2, 3, 4, 4, 5]

b = ['a', 'a', 'b', 'c']

print(duplicates(aa)) # [1, 2, 3, 4, 5]

print(duplicates(b)) # ['a', 'b', 'c']

second value finding in a list:


Output:45
a = [10, 20, 4, 45, 99]

# Sorting the list in descending order

[Link](reverse=True)

# Second largest number will be at index

print(a[1])

You might also like