='A' and letter"/> ='A' and letter">

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

Day String Functions and Recursion

Patricia Arquette
Patricia ArquetteOriginal
2024-12-28 00:03:17172browse

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='')
Today Is Friday

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='')

TodayIsFriday

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='')
Today Is Friday

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     

Today Is Friday

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  

Today Is Friday

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)
1 2 3 4 5

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)
24

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='')
Today Is Friday

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))

Enter the number:123
321

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")

Enter the number:1221
Palindrome
Enter the number:56878
Not palindrome

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)
0 1 1 2 3 5 8

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))

Enter no. 12
False

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!

Statement:
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