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:
+ operator. For example:
str1 = "Hello" str2 = "World" str3 = str1 + " " + str2 print(str3) # Output: "Hello World"
len() function. For example:
str = "Hello World" print(len(str)) # Output: 11
str = "Hello World" print(str[0]) # Output: "H" print(str[6:11]) # Output: "World"
upper() and lower() methods. For example:
str = "Hello World" print(str.upper()) # Output: "HELLO WORLD" print(str.lower()) # Output: "hello world"
replace() method. For example:
str = "Hello World"
new_str = str.replace("World", "Python")
print(new_str) # Output: "Hello Python"
{} and the format() 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.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
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:
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.
Most Viewed Articles (in Python )
Retrieve Twitter posts and comments using Python
Python program to get location meta data from an image
How to install Jupyter in Ubuntu and make it accessible through Apache Reverse Proxy
Installing gedit for python programming in Windows
Latest Articles (in Python)
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
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
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate