Python Strings

Python Strings

Python strings are sequences of characters, enclosed in either single quotes (”) or double quotes (“”). They are immutable, which means they cannot be modified once created. You can perform various operations on strings in Python, such as concatenation, slicing, formatting, and more.

Here are some examples of working with strings in Python:

  1. String creation:python
my_string = "Hello, World!"

Accessing characters:

python
print(my_string[0])  # Output: 'H'
print(my_string[-1])  # Output: '!'

String concatenation:

python
greeting = "Hello"
name = "Alice"
message = greeting + ", " + name + "!"
print(message)  # Output: "Hello, Alice!"

String slicing:

python
print(my_string[7:12])  # Output: "World"
print(my_string[:5])  # Output: "Hello"
print(my_string[7:])  # Output: "World!"

String length:

python
print(len(my_string))  # Output: 13

String methods:

python
print(my_string.upper())  # Output: "HELLO, WORLD!"
print(my_string.lower())  # Output: "hello, world!"
print(my_string.replace("Hello", "Hi"))  # Output: "Hi, World!"
print(my_string.split(","))  # Output: ['Hello', ' World!']

String formatting:

python
  1. name = "Bob" age = 25 formatted_string = "My name is {} and I'm {} years old.".format(name, age) print(formatted_string) # Output: "My name is Bob and I'm 25 years old." # Using f-strings (Python 3.6+) formatted_string = f"My name is {name} and I'm {age} years old." print(formatted_string) # Output: "My name is Bob and I'm 25 years old."

These are just a few examples of what you can do with strings in Python. Strings in Python have many more methods and functionalities, so feel free to explore the Python documentation for more information.

Leave a Comment

Your email address will not be published. Required fields are marked *

GLOTRU Footer
Popular Features
Popular Services/

Website Development & Design

App Development & Design

Graphic Design

Digital Marketing

SEO (Search Engine Optimization)

SMM (Social Media Marketing)

Cyber Security

Company

GLOTRU Founder & CEO : __Azam

Registared : Trade,MSME,etc

Board of Director

Team

About Us

Contact Us

Privacy Policy

Return & Refund Policy

Abuse Policy

Copyright Policy

Cookie Policy

Terms & Conditions

Universal Terms of Service

Disclaimer

Legal

Sponsorships

Investor

Press Releases

Our Investments

Brands

Newsroom

Business

...

_

Digital Millennium Copyright Act
DMCA.com Protection Status

_

Content similarity detection
Protected by Copyscape

_

***ANTI-PIRACY WARNING***

...................................................................................

Follow Us :

...................................................................................

SECURE SERVER : [Legal] [Privacy Policy] [Universal Terms of Service] [Do not sell my personal information]

SITE HOSTED : GLOTRU SECURE SERVER Asian Data Centre [You can host your site][Click Here]

SSL : Server Type : [Cloudflare] Certificate Issued By : [Let's Encrypt] Signature Algorithm : [ECDSA with SHA-384]

SITE BUILD SOFTWARE : Content Management System (CMS) Softwere

_

_

_