Day - String functions
1.Write a program to check given key is available or not:
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) break start+=1 end+=1 else: print('Not Contains')
Contains fruit
2.Write a program to find given key position:
find() returns the position of where it was found.
txt = "Python is my Favourite Language" key = 'Python' 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')
Contains Python 0 5
3.Write a program to check the word starts with the specified key:
startswith() check if the string starts with the specified value.
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')
Starts with Python
Another way to check the word starts with the specified key:
txt = "Apples are good, apple is my favorite fruit" key = 'Apple' l = len(key) if txt[0:l]==key: print('Starts with',key)
Starts with Apple
4.Write a program to check the word ends with the specified key:
endswith() check if the string ends with the specified value.
txt = "My Favourite Language is Python" key = 'Python' 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 Contains')
Ends with Python
Another way to check the word ends with the specified key:
txt = "Apples are good, apple is my favorite fruit" key = 'fruit' l = len(key) if txt[-l:]==key: print('Ends with',key)
Ends with fruit
5.Write a program to check the given word is alpha or not:
isalpha() check all characters in the string are alphabet.
alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' word = 'abcdEFGH' for letter in word: if letter not in alpha: print('Not all are alphabets') break else: print('All are alphabets')
All are alphabets
6.Write a program to check the given word is alnum or not:
isalnum() checks all characters in the string are alphanumeric.
alpha = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' word = 'pritha017@gmail.com' for letter in word: if letter not in alpha: print('Not all are alphabets and numbers') break else: print('All are alphabets and numbers')
Not all are alphabets and numbers
7.Write a program to check the given word is lowercase or not:
islower() checks all characters in the string are lower case.
alpha = 'abcdefghijklmnopqrstuvwxyz' word = 'lakshmipritha' for letter in word: if letter not in alpha: print('Not all are lowercase alphabets') break else: print('All are lowercase alphabets')
All are lowercase alphabets
8.Write a program to check the given word is uppercase or not:
isupper() checks all characters in the string are upper case.
alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' word = 'LAKSHMIPRITHA' for letter in word: if letter not in alpha: print('Not all are uppercase alphabets') break else: print('All are uppercase alphabets')
All are uppercase alphabets
9.Write a program to check space is available or not:
isspace() checks spaces in the text.
word = ' ' for letter in word: if letter != ' ': print("Not all are spaces") break else: print('All are spaces')
All are spaces
Task:
1.Write a program to convert Uppercase into lowercase:
lower() converts a string into lower case.
txt = "HELLO, AND WELCOME TO MY WORLD!" for letter in txt: if letter>='A' and letter<'Z': letter = ord(letter)+32 letter = chr(letter) print(letter,end='')
hello, and welcome to my world!
2.Write a program to convert lowercase into uppercase:
upper() converts a string into upper case.
txt = "hello, and welcome to my world!" for letter in txt: if letter>='a' and letter<'z': letter = ord(letter)-32 letter = chr(letter) print(letter,end='')
HELLO, AND WELCOME TO MY WORLD!
3.Write a program to check the given string title or not:
istitle() checks the string follows the rules of a title.
The above is the detailed content of Day - String functions. 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

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

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

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

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

Using python in Linux terminal...

Fastapi ...

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