


1) To add space between strings
txt = "TodayIsFriday" #Today is Friday first = True for letter in txt: if letter>='A' and letter <p>Output:<br> Today Is Friday</p> <p><strong>2)To remove space between strings</strong><br> </p> <pre class="brush:php;toolbar:false">txt = " Today Is Friday" #Today is Friday for letter in txt: if letter==' ': pass else: print(letter,end='')
Output:
TodayIsFriday
3) ltrim- To remove spaces in left side of the strings.
#ltrim txt = " Today Is Friday" #Today is Friday alphabet = False for letter in txt: if letter==' ' and alphabet == False: pass else: alphabet = True print(letter,end='')
4) rtrim- To remove spaces in right side of the strings.
txt = "Today Is Friday " #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 <p>Output:</p> <p>Today Is Friday</p> <p><strong>5) Removing Unwanted spaces from given String</strong><br> </p> <pre class="brush:php;toolbar:false">txt = "Today Is Friday" #Today is Friday i = 0 while i<len if txt print else: i> <p>Output:</p> <p>Today Is Friday</p> <p><strong>Recursion:</strong><br> Function calling itself.</p> <p>Looping-->Iterative approach.<br> Recursion-->Recursive approach.</p> <p>Example:1<br> </p> <pre class="brush:php;toolbar:false">def display(no): print(no, end=' ') no+=1 if no <p>Output:<br> </p> <pre class="brush:php;toolbar:false">1 2 3 4 5
Recursive function for calling factorial:
5!=5x4x3x2x1 (or) 5x4!
4!=4x3x2x1 (or) 4x3!
3!=3x2x1 (or) 3x2!
2!=2x1 (or) 2x1!
1!=1
Example:2
def find_fact(no): if no==1: return 1 return no * find_fact(no-1) result = find_fact(5) print(result)
Output:
120
Explanation:
1) find_fact(5)
Returns 5 * find_fact(4) #no-1 = 5-1 -->4
2) find_fact(4)
Returns 4 * find_fact(3) #no-1 = 4-1 -->3
3) find_fact(3)
Returns 3 * find_fact(2) #no-1 = 3-1 -->2
4) find_fact(2)
Returns 2 * find_fact(1) #no-1 = 2-1 -->1
5) find_fact(1)
Base case: Returns 1
Base case: The base case in recursion is a condition that stops the recursive calls.
Tasks:
strip()-Removes all white space characters (spaces, tabs, newlines) from the start and end of the string.
1) Remove unwanted spaces in front and back of the given string.
txt = " Today Is Friday " start = 0 end = len(txt) - 1 while start = 0: i = start while i = 0 and txt[j] == ' ': j -= 1 end = j break result = txt[start:end+1] print(result)
Output:
Today Is Friday
2) Reversing a number using recursive function:
def reverse_a_no(no,reverse = 0): if no==0: return reverse rem = no%10 reverse = (reverse*10) + rem no=no//10 return reverse_a_no(no,reverse) no = int(input("Enter no. ")) reversed_no = reverse_a_no(no) print(reversed_no)
Output:
Enter no. 15 51
3)Find prime number or not:
def find_prime(no,div=2): if div<no: if no return false div find_prime else: true the number: print number emirp> <p>Output:<br> </p> <pre class="brush:php;toolbar:false">1) Enter the number: 11 EMIRP number 2) Enter the number: 15 not EMIRP number
4) Find fibonacci:
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)
Output:
Enter the number: 10 0 1 1 2 3 5 8
5. Find palindrome or not:
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")
Output:
Enter the number:121 Palindrome
Created HackerRank Account: https://www.hackerrank.com/dashboard
The above is the detailed content of Python Day-String Functions logic using loops, Recursion, Tasks. For more information, please follow other related articles on the PHP Chinese website!

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i

Forloopsareadvantageousforknowniterationsandsequences,offeringsimplicityandreadability;whileloopsareidealfordynamicconditionsandunknowniterations,providingcontrolovertermination.1)Forloopsareperfectforiteratingoverlists,tuples,orstrings,directlyacces

Pythonusesahybridmodelofcompilationandinterpretation:1)ThePythoninterpretercompilessourcecodeintoplatform-independentbytecode.2)ThePythonVirtualMachine(PVM)thenexecutesthisbytecode,balancingeaseofusewithperformance.

Pythonisbothinterpretedandcompiled.1)It'scompiledtobytecodeforportabilityacrossplatforms.2)Thebytecodeistheninterpreted,allowingfordynamictypingandrapiddevelopment,thoughitmaybeslowerthanfullycompiledlanguages.

Forloopsareidealwhenyouknowthenumberofiterationsinadvance,whilewhileloopsarebetterforsituationswhereyouneedtoloopuntilaconditionismet.Forloopsaremoreefficientandreadable,suitableforiteratingoversequences,whereaswhileloopsoffermorecontrolandareusefulf

Forloopsareusedwhenthenumberofiterationsisknowninadvance,whilewhileloopsareusedwhentheiterationsdependonacondition.1)Forloopsareidealforiteratingoversequenceslikelistsorarrays.2)Whileloopsaresuitableforscenarioswheretheloopcontinuesuntilaspecificcond


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
