Python Day-String functions logic using loops,Task
1) find(): Searches the string for a specified value and returns the position of where it was found.
txt = "I love many fruits, apple is my favorite fruit" key = 'fruit' l = len(key) start = 0 end = l while end<=len(txt): if txt[start:end] == key: print('Contains', key) print(start, end-1) break start+=1 end+=1 else: print('Not Contains')
Output:
Contains fruit 12 16
2) startswith(): Returns true if the string starts with the specified value
Example:1
#starts with: txt = "Python is my favourite language" key = 'Python' l = len(key) start = 0 end = l while end<len(txt): if txt[start:end] == key: if start == 0: print("Starts with",key) break start+=1 end+=1 else: print('Not Contains')
Output:
Starts with Python
Example:2
txt = "Apples are good, apple is my favorite fruit" key = 'Apple' #starts with l = len(key) #5 if txt[0:l] == key: print('Starts with',key)
Output:
Starts with Apple
3) endswith(): Returns true if the string ends with the specified value.
Example:1
txt = "Apples are good, apple is my favorite fruit" key = 'fruit' #starts with l = len(key) #5 if txt[-len(key):] == key: print('Ends with',key)
Output:
Ends with fruit
Example:2
txt = "Python is my favourite language" key = 'language' l = len(key) start = 0 end = l while end<=len(txt): if txt[start:end] == key: if end==len(txt): print("Ends with",key) break start+=1 end+=1 else: print('Not ending with language')
Output:
Ends with language
4) isalpha(): Returns True if all characters in the string are in the alphabet.
Method:1
word = 'abcdEFGH' for letter in word: if letter>='a' and letter<='z' or letter>='A' and letter<='Z': continue else: print('not all are alphabets') break else: print('All are alphabets')
Method:2
alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' word = 'abcdEFGH' for letter in word: if letter not in alpha: print('Not all are alphabets') break else: print('All are alphabets')
Output:
All are alphabets
5) isalnum(): Returns True if all characters in the string are alphanumeric.
#isalnum alpha = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' word = 'abcd1234' for letter in word: if letter not in alpha: print('Not all are alphabets and numbers') break else: print('All are alphabets and numbers')
Output:
All are alphabets and numbers
6) islower(): Returns True if all characters in the string are lower case.
#islower alpha = 'abcdefghijklmnopqrstuvwxyz' word = 'lakshmipritha' for letter in word: if letter not in alpha: print('Not all are lower alphabets') break else: print('All are lower alphabets')
Output:
All are lower alphabets
7) isupper(): Returns True if all characters in the string are upper case.
#isupper alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' word = 'GURU' for letter in word: if letter not in alpha: print('Not all are UPPERCASE alphabets') break else: print('All are UPPERCASE alphabets')
Output:
All are UPPERCASE alphabets
8) isspace(): Returns True if all characters in the string are whitespaces.
#isspace word = ' ' for letter in word: if letter != ' ': print("not all are spaces") break else: print('All are spaces')
Output:
All are spaces
Tasks:
1) lower(): Converts a string into lower case.
txt = "PYTHON IS MY FAVOURITE LANGUAGE" for letter in txt: if letter>='A' and letter<='Z': letter = ord(letter)+32 letter = chr(letter) print(letter,end='')
Output:
python is my favourite language
2) upper(): Converts a string into upper case.
txt = "python is my favourite language" for letter in txt: if letter>='a' and letter<='z': letter = ord(letter)-32 letter = chr(letter) print(letter,end='')
Output:
PYTHON IS MY FAVOURITE LANGUAGE
The above is the detailed content of Python Day-String functions logic using loops,Task. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Fastapi ...

Using python in Linux terminal...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

About Pythonasyncio...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

Discussion on the reasons why pipeline files cannot be written when using Scapy crawlers When learning and using Scapy crawlers for persistent data storage, you may encounter pipeline files...
