python string
By: Ashley J in Python Tutorials on 2017-09-27
Python String objects (byte strings, as well as text, AKA Unicode, ones) are immutable: attempting to rebind or delete an item or slice of a string will raise an exception. The items of a string object (corresponding to each of the characters in the string) are themselves strings of the same kind, each of length 1.
Unicode str and bytes objects are immutable sequences. All immutable-sequence operations (repetition, concatenation, indexing, and slicing) apply to them, returning an object of the same type. Listed below are some methods available for string handling in Python.
Here are some common string operations in Python:
- Concatenation: You can concatenate two or more strings using the
+
operator. For example:str1 = "Hello" str2 = "World" str3 = str1 + " " + str2 print(str3) # Output: "Hello World"
- Length: You can get the length of a string using the
len()
function. For example:str = "Hello World" print(len(str)) # Output: 11
- Indexing and Slicing: You can access individual characters of a string using indexing, and a range of characters using slicing. For example:
str = "Hello World" print(str[0]) # Output: "H" print(str[6:11]) # Output: "World"
- Case Conversion: You can convert the case of a string using the
upper()
andlower()
methods. For example:str = "Hello World" print(str.upper()) # Output: "HELLO WORLD" print(str.lower()) # Output: "hello world"
- String Replacement: You can replace a substring within a string using the
replace()
method. For example:str = "Hello World" new_str = str.replace("World", "Python") print(new_str) # Output: "Hello Python"
- String Formatting: You can format a string using placeholders
{}
and theformat()
method. For example:name = "Alice" age = 30 print("My name is {} and I am {} years old.".format(name, age)) # Output: "My name is Alice and I am 30 years old."
These are just a few of the many string operations available in Python.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Python program to get location meta data from an image
Retrieve Twitter posts and comments using Python
How to install Jupyter in Ubuntu and make it accessible through Apache Reverse Proxy
Python Basics - Setting up your Python Development Environment
Schwartzian Transform in python
Multidimensional list (array) in python
Perl's chomp() equivalent for removing trailing newlines from strings in python
Comments