Home > Backend Development > Python Tutorial > Day String Functions and Recursion

Day String Functions and Recursion

Patricia Arquette
Release: 2024-12-28 00:03:17
Original
167 people have browsed it

Day  String Functions and Recursion

1.Write a program to add space between the strings.

txt = "TodayIsFriday"
first = True
for letter in txt:
    if letter>='A' and letter<='Z':
        if first==True:
            first = False
        else:
            print(' ',end='')
    print(letter,end='')
Copy after login
Today Is Friday
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

2.Write a program to remove space between the strings.

txt = "    Today Is Friday"
for letter in txt:
    if letter==' ':
        pass
    else:
        print(letter,end='')

Copy after login
TodayIsFriday
Copy after login

3.Write a program to remove space in the left side of the string:

ltrim()-to remove any leading whitespace or specified characters from the left side of a string.

txt = "    Today Is Friday"
alphabet = False
for letter in txt:
    if letter==' ' and alphabet==False:
        pass
    else:
        alphabet = True
        print(letter,end='')
Copy after login
Today Is Friday
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

4.Write a program to remove space in the right side of the string:

rtrim()-to remove any leading whitespace or specified characters from the right side of a string.

txt = "Today Is Friday   "
alphabet = False
i = len(txt)-1
while i>=0:
    letter = txt[i]
    if letter==' ' and alphabet == False:
        pass
    else:
        alphabet = True
        end = i
        j = 0
        while j<=end:
            print(txt[j],end='')
            j+=1
        break
    i-=1     

Copy after login
Today Is Friday
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

5.Write a program to remove unwanted space from the given string:

txt = "Today              Is                       Friday"
i = 0 
while i<len(txt):
    if txt[i] != ' ':
        print(txt[i],end='')
    else:
        if txt[i-1]!=' ':
            print(txt[i],end='')
    i+=1  

Copy after login
Today Is Friday
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

Recursion:
Function calling itself.

what is function?
Set if instructions with a name for achieving a specific task.

Looping-Iterative approach.
Recursion-Recursive approach.

Example:

def display(no):
    print(no, end=' ')
    no+=1
    if no<=5:
        display(no)
display(1)
Copy after login
1 2 3 4 5
Copy after login

Write a factorial program using recursion:

def find_fact(no):
    if no==1:
        return 1
    return no * find_fact(no-1)

result = find_fact(4)
print(result)
Copy after login
24
Copy after login

Task:
Write a program to remove unwanted space from the given string:

strip()-Removes all white space characters from the start and end of the string.

txt = "    Today Is Friday    "
first=True
for letter in txt:
    if letter==" ":
        pass
    else:
        if letter>='A' and letter<='Z':
            if first==True:
                first = False
            else:
                print(' ',end='')
        print(letter,end='')
Copy after login
Today Is Friday
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

Write a program to given number in reverse order using recursion:

def reverse_number(num,reverse=0):
    if num==0:
        return reverse
    return reverse_number(num//10,reverse*10+num%10)
num=int(input("Enter the number:"))
print(reverse_number(num))

Copy after login
Enter the number:123
321
Copy after login

Write a program to find the given number is palindrome or not using recursion:

def palindrome(num,count=0):
    if num==0:
        return count
    return palindrome(num//10,count*10+num%10)

num=int(input("Enter the number:"))
result=palindrome(num)
if result==num:
    print("Palindrome")
else:
    print("Not palindrome")

Copy after login
Enter the number:1221
Palindrome
Enter the number:56878
Not palindrome
Copy after login

Write a program to find fibonacci number using recursion:

def find_fibonacci(first_num,sec_num,no):
    if first_num > no:
        return
    print(first_num, end=" ")

    find_fibonacci(sec_num,first_num+sec_num,no)      

no = int(input("Enter the number: ")) 
find_fibonacci(0,1,no)
Copy after login
0 1 1 2 3 5 8
Copy after login

write a program to find prime number using recursion:

def find_prime(no,div=2):
    if div<no:
        if no%div == 0:
            return False
        div+=1
        return find_prime(no,div)
    else:
        return True

no = int(input("Enter no. "))
print(find_prime(no))

Copy after login
Enter no. 12
False
Copy after login

The above is the detailed content of Day String Functions and Recursion. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template