Categories
5G Network
Agile
Amazon EC2
Android
Angular
Ansible
Arduino
Artificial Intelligence
Augmented Reality
AWS
Azure
Big Data
Blockchain
BootStrap
Cache Teachniques
Cassandra
Commercial Insurance
C#
C++
Cloud
CD
CI
Cyber Security
Data Handling
Data using R
Data Science
DBMS
Design-Pattern
DevOps
ECMAScript
Fortify
Ethical Hacking
Framework
GIT
GIT Slack
Gradle
Hadoop
HBase
HDFS
Hibernate
Hive
HTML
Image Processing
IOT
JavaScript
Java
Jenkins
Jira
JUnit
Kibana
Linux
Machine Learning
MangoDB
MVC
NGINX
Onsen UI
Oracle
PHP
Python
QTP
R Language
Regression Analysis
React JS
Robotic
Salesforce
SAP
Selenium
Service Discovery
Service Now
SOAP UI
Spark SQL
Testing
TOGAF
Research Method
Virtual Reality
Vue.js
Home
Recent Q&A
Feedback
Ask a Question
what is python strings
Home
>
Python
>
what is python strings
Dec 14, 2019
in
Python
Q: python strings
python-strings
1
Answer
0
votes
Dec 14, 2019
Till now, we have discussed numbers as the standard data types in python. In this section of the tutorial, we will discuss the most popular data type in python i.e., string.
In python, strings can be created by enclosing the character or the sequence of characters in the quotes. Python allows us to use single quotes, double quotes, or triple quotes to create the string.
Consider the following example in python to create a string.
str = "Hi Python !"
Here, if we check the type of the variable str using a python script
print(type(str)), then it will print string (str).
In python, strings are treated as the sequence of strings which means that python doesn't support the character data type instead a single character written as 'p' is treated as the string of length 1.
Strings indexing and splitting
Like other languages, the indexing of the python strings starts from 0. For example, The string "HELLO" is indexed as given in the below figure.
Python String
As shown in python, the slice operator [] is used to access the individual characters of the string. However, we can use the : (colon) operator in python to access the substring. Consider the following example.
Python String
Here, we must notice that the upper range given in the slice operator is always exclusive i.e., if str = 'python' is given, then str[1:3] will always include str[1] = 'p', str[2] = 'y', str[3] = 't' and nothing else.
Reassigning strings
Updating the content of the strings is as easy as assigning it to a new string. The string object doesn't support item assignment i.e., A string can only be replaced with a new string since its content can not be partially replaced. Strings are immutable in python.
Consider the following example.
Example 1
str = "HELLO"
str[0] = "h"
print(str)
Output:
Traceback (most recent call last):
File "12.py", line 2, in <module>
str[0] = "h";
TypeError: 'str' object does not support item assignment
However, in example 1, the string str can be completely assigned to a new content as specified in the following example.
Example 2
str = "HELLO"
print(str)
str = "hello"
print(str)
Output:
HELLO
hello
Click here to read more about Python
Click here to read more about Insurance
Facebook
Twitter
LinkedIn
Related questions
0
votes
Q: What is the best all-purpose way of comparing two strings?
Mar 19, 2020
in
PHP
#php
0
votes
Q: What is the difference between Strings and Arrays in C Language?
Mar 12, 2020
in
C Plus Plus
#c-language-string-vs-array
0
votes
Q: Which string method is used for concatenation of two strings in c#?
Oct 18, 2019
in
C Sharp
c#-concatenation
0
votes
Q: Apart from the security aspect, what are the reasons behind making strings immutable in Java?
Dec 16, 2020
in
JAVA
#security-aspect
#strings
#java-strings
0
votes
Q: What is pickling and unpickling in Python?
Jan 11
in
Python
#pickling-python
0
votes
Q: What is module and package in Python?
Jan 1
in
Python
#python-package
...