String Functions

Nov 19, 2024 pm 06:48 PM

String Functions

Python string functions:

Python has a set of built-in methods that you can use on strings.

All string methods returns new values. They do not change the original string.

1.**capitalize(): **Capitalizes the first character of the string.

name = "pritha"
print(name.capitalize()) 
Copy after login
Pritha
Copy after login

2.casefold():Converts string into lower case

name = "PRITHA"
print(name.casefold()) 
Copy after login
pritha
Copy after login

3.center():Returns a centered string

name = "pritha"
print(name.center(10,"-")) 
Copy after login
--pritha--
Copy after login

4.count():Returns the number of times a specified value occurs in a string

name = "lakshmipritha"
print(name.count('a')) 
Copy after login
2
Copy after login

5.encode():Returns an encoded version of the string

name = "lakshmipritha"
print(name.encode()) 
Copy after login
b'lakshmipritha'
Copy after login

6.endswith():Returns true if the string ends with the specified value

name = "lakshmi pritha"
print(name.endswith('pritha')) 
Copy after login
True
Copy after login

7.find():Searches the string for a specified value and returns the position of where it was found

name = "lakshmi pritha"
print(name.find('pritha')) 
Copy after login
8
Copy after login
Copy after login

8.format():Formats specified values in a string

name = "Hello, {}. Welcome to {}."
print(name.format("Pritha", "Python")) 
Copy after login
Hello, Pritha. Welcome to Python.
Copy after login

9.format_map():Formats specified values in a string

text = "My name is {name} and I am {age} years old."
data = {"name": "Pritha", "age":30 }
print(text.format_map(data))

Copy after login
My name is Pritha and I am 30 years old.
Copy after login

10.index():Searches the string for a specified value and returns the position of where it was found

name= "lakshmi pritha"
position = name.index("pritha")
print(position)
Copy after login
8
Copy after login
Copy after login

11.isalnum():Returns True if all characters in the string are alphanumeric

12.isalpha():Returns True if all characters in the string are in the alphabet

13.isascii():Returns True if all characters in the string are ascii characters

14.isdecimal():Returns True if all characters in the string are decimals

15.isdigit():Returns True if all characters in the string are digits

16.isidentifier():Returns True if the string is an identifier

17.islower():Returns True if all characters in the string are lower case

18.isnumeric():Returns True if all characters in the string are numeric

19.isprintable():Returns True if all characters in the string are printable

20.isspace():Returns True if all characters in the string are whitespaces

21.istitle():Returns True if the string follows the rules of a title

22.isupper():Returns True if all characters in the string are upper case

name = "pritha"
print(name.isalnum())
print(name.isalpha())
print(name.isascii())
print(name.isdecimal())
print(name.isdigit())
print(name.isidentifier())
print(name.islower())
print(name.isnumeric())
print(name.isprintable())
print(name.isspace())
print(name.istitle())
print(name.isupper())

Copy after login
True
True
True
False
False
True
True
False
True
False
False
False





Copy after login

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

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 by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

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

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

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 without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

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 solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

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

How to get news data bypassing Investing.com's anti-crawler mechanism? How to get news data bypassing Investing.com's anti-crawler mechanism? Apr 02, 2025 am 07:03 AM

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

See all articles