# Strings In Python

![](https://767344992-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMiliykuSLaUcLgGbj1qR%2Fuploads%2FYnbezMMJ5dRerYrG1FFl%2Fimage.png?alt=media\&token=a0756f0c-cba4-4fcd-84b6-71ef8836bab6)

## What is String?

Python string is the collection or sequence of the characters surrounded by single quotes(' '), double quotes(" "), or triple quotes(''' '''),(""" """).

In more simpler terms , whatever you write between these quotes are String. For Example:\</span>

In \[ ]:

```python
Company = "Console Flare"
member = '500'
year = '''4'''
Sector = """IT"""
print("Data type of company: ",type(Company))
print("Data type of member: ",type(member))
print("Data type of year: ",type(year))
print("Data type of sector: ",type(Sector)) 
```

![](https://767344992-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMiliykuSLaUcLgGbj1qR%2Fuploads%2F3IkrIhNcoH2OOtXwtfoL%2Fimage.png?alt=media\&token=ded13bef-af39-450b-9ccf-3fe88dbabea7)

## Single Line String Vs Multiline String:

Single line strings are written under single quotes '' and double quotes "". They appear as a single line on console screen. For Example:

In \[3]:

```python
Sentence = "Python is a General Purpose programming language"
print(Sentence)
```

```
Python is a General Purpose programming language
```

Multiline line strings are written under triple quotes ''' ''' and """ """. They appear in multiple lines such as paragraphs. For Example:

In \[6]:

```python
Sentence = '''Python is 
a general purpose
Programming Language'''
print(Sentence)
```

```
Python is 
a general purpose
Programming Language
```

## Memory Allocation of Numbers Vs Strings:

Memory allocation of Numbers : A number (integer or float) gets stored in a memory block and gets associated with a variable as soon as we define it. For Example:

In \[ ]:

```python
age = 24
```

![](https://767344992-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMiliykuSLaUcLgGbj1qR%2Fuploads%2FIJQTebEBzgTGxgTyUv9z%2Fimage.png?alt=media\&token=3a2b9059-937d-4c8d-982a-3761cf32131e)

Memory Allocation of Strings : As we know that strings are a sequence of characters, hence Each character of string gets store in different memory block, something like this:

In \[ ]:

```python
name = "ABHI"
```

![](https://767344992-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMiliykuSLaUcLgGbj1qR%2Fuploads%2FlVHGWvMwJQrz2WYCC8Ev%2Fimage.png?alt=media\&token=71e1445b-e3a7-48b9-a3ef-289b851010da)

## Indexing of Strings:

As we know that characters of strings are stored in different memory blocks, each character within a string is given an index number.

Python offers two different indexing :

Positive Index Numbers : Positive index numbers starts from zero (0) and left to right.

## Positive Index Numbers :  <a href="#positive-index-numbers" id="positive-index-numbers"></a>

![](https://767344992-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMiliykuSLaUcLgGbj1qR%2Fuploads%2FOUpA2ZXQrL5N2QJKHaMb%2Fimage.png?alt=media\&token=af77a9ad-1095-43d2-a434-9ea79c8d3819)

## To Access characters from a string (+ve indexing):

As we know that characters of a string are identified by their index number, so we can access characters with the help of character's index numbers.

For Example in name = 'abhi', to get character 'a' from a string 'abhi' , we will type name\[0].

In \[8]:

```python
name = "abhi"
name[0]
```

```
a
```

## Negative Index Numbers:

![](https://767344992-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMiliykuSLaUcLgGbj1qR%2Fuploads%2F1RXjjINmWocBAtkgyHNr%2Fimage.png?alt=media\&token=e4cdbb82-0f35-4b77-87c4-d89f22630923)

## To Access Characters from string (-ve indexing):

Suppose there is a long string and we need to access character at the end of the string, instead of using positive indexing , negative indexing would be much more helpful. For Example:

In \[10]:

```python
sentence = "Python is a Genreal Purpose Programming LanguagE"
```

To get 'E' from sentence with the help of Positive indexing would be tough rather than accessing character with the help of negative indexing.

In \[11]:

```python
sentence[-1]
```

Out\[11]:

```
'E'
```

## Slicing of a String:

Slicing of a string means taking a substring or part of a string from it. For example , 'Console' is a substring of 'Console Flare'. Index numbers should always be in ascending orders.

Slicing of a string is done like this : variable\[starting index : ending index]. Here ending index should be one index number more than the last character's index number.

In more simpler terms in name = "abhishek", "abhi" from "abhishek" can be sliced as name\[0:4].

In \[12]:

```python
company = "Console Flare"
company[0:7]
```

Out\[12]:

```
'Console'
```

![](https://767344992-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMiliykuSLaUcLgGbj1qR%2Fuploads%2FsOdmJVJDxlskmWsJpGpn%2Fimage.png?alt=media\&token=4cf2d7a4-0dd4-4b1b-a03c-e01f5dd8cd97)

## Slicing of string with steps:

variable\[starting index : ending index : step size(n)], here steps mean, it takes every n step starting from starting index. Default value of step is 1.

In \[13]:

```python
company = "console flare"
company[0:14:2]
```

Out\[13]:

```
'cnoefae'
```

## Default Value of Starting Index , Ending index and Step size:

Default Value of Starting index is 0.If we don't write anything as Starting index while slicing , it takes 0 as default. For Example:

In \[14]:

```python
company = "Console Flare"
company[:7]
```

Out\[14]:

```
'Console'
```

Default Value of Ending index is end of string.If we don't write anything as ending index while slicing , it takes end of string as indexing. For Example:

In \[15]:

```python
company = "Console Flare"
company[0:]
```

Out\[15]:

```
'Console Flare'
```

Default Value of Step size is 1 and is optional , we need to use step size only if we want it to skip characters in a string.

In \[16]:

```python
company = "Console Flare"
company[::3]
```

Out\[16]:

```
'Csele'
```

## Negative Slicing of a string:

Negative slicing of a string means to slice the string with the help of negative indexes. Here also , index numbers should always be in ascending order. For Example:

In \[18]:

```python
company = "console flare"
company[-13:-6]
```

Out\[18]:

```
'console'
```

## String formatting:

formatting strings lets you use embedded Python expressions inside string. Here’s a simple example to give you a feel for the feature:

In \[86]:

```python
birthyear = 1997
presentage = f'my present age is {2021-birthyear}'
print(presentage)
```

```
my present age is 24
```

f'string and expressions': This is how you can merge expressions and string in a single string.

## Functions used in String :

**len() function:**

len() function is used to count characters in a string. For Example:In \[19]:

```python
sentence = "String is a sequence of characters"
len(sentence)
```

Out\[19]:

```
34
```

**max() function:**

max() function is used to return largest value or character in a string , For Example:In \[22]:

```python
sentence = '1234542398'
max(sentence)
```

Out\[22]:

```
'9'
```

max() function can also return largest alphabetic character based on their ascii code. You can go through ASCII codes of different alphabets through [here](http://sticksandstones.kstrom.com/appen.html). For Example:

In \[23]:

```python
sentence = 'Console Flare'
max(sentence)
```

Out\[23]:

```
's'
```

ASCII Code of 's' is 115 , largest than any other character's.

**min() function:**

min() function is used to return smallest value or character in a string , For Example:In \[25]:

```python
sentence ='123'
min(sentence)
```

Out\[25]:

```
'1'
```

## String Methods :

To Understand String Methods better , first we will have to know what are methods.

#### **What are Methods?**

Methods are nothing but **Type - Specific Function.** Methods are functions that only work for a particular data type.

In more Simpler terms, String methods are functions that will only work for strings. String methods are used for handling strings , performing operations on string , string manipulation etc.

**Note:** <mark style="color:red;">All String methods return new values and does not change original string.</mark>

## 1.lower() Method:

lower() method is used to return string with all its character in lowercase. We do not pass any paramters in lower(). For Example:

In \[27]:

```python
company = "CONSOLE FLARE"
var = company.lower()
print(var)
```

```
console flare
```

## 2.upper() Method:

upper() method is used to return string with all its character in Capital case. We do not pass any paramters in upper(). For Example:

In \[28]:

```python
company = "console flare"
var = company.upper()
print(var
```

```
CONSOLE FLARE
```

## 3.swapcase() Method:

swapcase() method is used to return string with all its character in lower to upper case and all its string in upper to lower case. It does not have any parameters. For Example:

In \[29]:

```python
company = "CoNSole FLAre"
var = company.swapcase()
print(var)
```

```
cOnsOLE flaRE
```

## 4.title() Method :

The title () method in python is used to convert the first character in each word to Uppercase and remaining characters to Lowercase in the string and returns a new string. It does not have any paramters. For Example:

In \[31]:

```python
sentence = "pyThon is a geneRAL PuRPOSE programming Language.It is invented by Guido van rossum."
var = sentence.title()
print(var)
```

```
Python Is A General Purpose Programming Language.It Is Invented By Guido Van Rossum.
```

## 5.capitalize() Method:

capitalize() method return string with its first character as upper case and rest in lower case. It does not have any parameters. For Example:

In \[32]:

```python
sentence = "pyThon is a geneRAL PuRPOSE programming Language.It is invented by Guido van rossum."
var = sentence.capitalize()
print(var)
```

```
Python is a general purpose programming language.it is invented by guido van rossum.
```

## 6.strip(),lstrip() and rstrip() Method:

strip() method removes all the whitespace from left and right side of the string.

lstrip() method removes all the whitespace from left side of the string.

rstrip() method removes all the whitespace from right side of the string.

In \[81]:

```python
company = "  console flare  "
var = company.strip()
print(company)
print(var)
```

```
  console flare  
console flare
```

In \[82]:

```python
company = "  console flare  "
var = company.lstrip()
print(company)
print(var)
```

```
  console flare  
console flare  
```

It removed all whitespaces from left side of string.

In \[84]:

```python
company = "console flare  "
var = company.rstrip()
print(company)
print(var)
```

```
console flare  
console flare
```

It removed all whitespaces from right side of string.

## 7.replace() Method:

replace() method returns a string where a specified value is replace with a specified value.

Parameters to be passed in replace:

oldvalue: The string to be replaced. It is **Required**

newvalue : The string to replace the old value with. It is **Required**

count: An integer value specifying how many occurences of the old string you want to replace. Default value of count is **All Occurences**. It is **Optional**

***replace(oldvalue,newvalue,count)***

In \[35]:

```python
sentence = "I am learning C Language C Language."
```

To replace 'C Language' with 'Python Programming Language' , we will use replace() method.

In \[36]:

```python
sentence = sentence.replace('C Language','Python Programming Language')
print(sentence)
```

```
I am learning Python Programming Language Python Programming Language.
```

To see , How count works, i will be using count parameter in below example:

In \[37]:

```
sentence = "I am learning C Language C Language."
sentence = sentence.replace('C Language','Python Programming Language',1)
print(sentence)
```

```
I am learning Python Programming Language C Language.
```

## 8.count() method:

count() method returns the number of times a specified value occurs in a string.

Parameters to be passed in count():

value : substring or value to count. It is **Required.**

start index : The position from where to start the search. It is **Optional**. Default value is 0.

end index : The position from where to end the search.It is **Optional**. Default value is the end of the string.

***count(value,start,end)***

In \[41]:

```
sentence = "Python is Python and C is C"
var = sentence.count("Python")
print(var)
```

```
2
```

To see , How start and end paramters work , i will be using them in below example:

In \[40]:

```
sentence = "Python is Python and C is C"
var = sentence.count("Python",1,16)
print(var)
```

```
1
```

## 9.find() Method:

find() method searches the string for a specified value and returns the starting position of where it is found. It searches the whole string for specified value from left to right.

In more simpler terms, it finds the string's position.

Parameters to be passed inside find() method:

value : The values to search for. It is **Required.**

start : Starting index, from where to look for the value. It is **Optional**

end : Ending index, from where to stop the search for value. It is **Optional**

***find(value,start,end)***

In \[45]:

```
sentence =  "Python is general purpose programming language."
lookfor = sentence.find("Python")
print(lookfor)
```

```
0
```

To see , How start and end paramters work , i will be using them in below example:

In \[47]:

```
sentence =  "Python is Python and C is C"
lookfor = sentence.find("Python",1,19)
print(lookfor)
```

```
10
```

Hence, the operation of start and end is same everywhere, it looked for 'python' from index number 1 to 18.

## 10.rfind() method:

rfind() method is same as find() method with same parameters. The only difference is it looks for specified string from right to left. For Example:

In \[49]:

```
sentence =  "Python is Python and C is C"
lookfor = sentence.rfind("Python")
print(lookfor)
```

```
10
```

## 11.index() and rindex() method:

index() method also searches the string for a specified value and returns the position of where it is found.

In more simpler terms index() works like find() and rindex() works like rfind(). They both have same parameters. For Example:

In \[50]:

```
sentence =  "Python is Python and C is C"
lookfor = sentence.index("Python")
print(lookfor)
```

```
0
```

In \[51]:

```
sentence =  "Python is Python and C is C"
lookfor = sentence.rindex("Python")
print(lookfor)
```

```
10
```

## Difference between find() and index() Method:

find() does not give error, when value or substring is not found and flow of the program does not interrupt.

index() gives error when value or substring is not found. for Example:

In \[53]:

```
sentence =  "Python is Python and C is C"
lookfor = sentence.find("Java")
print(lookfor)
```

```
-1
```

In \[55]:

```
sentence =  "Python is Python and C is C"
lookfor = sentence.index("Java")
print(lookfor)
```

```
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-55-7829979dbdb0> in <module>
      1 sentence =  "Python is Python and C is C"
----> 2 lookfor = sentence.index("Java")
      3 print(lookfor)

ValueError: substring not found
```

## 12.split() and rsplit() Method :

split() method splits the string at the specified separator and returns a list(We will cover list in upcoming lessons. It is another data type.)

Parameters to be passed in split():

separator : Specifies the separator to use when splitting the string. It is **Optional.**&#x42;y Default , any whitespace is separator.

maxsplit : maxsplit specifies how many splits to do . It is **Optional**.It's Default value is -1 or **All Occurences**

In \[56]:

```
sentence = "Python is a general purpose programming language"
var = sentence.split(" ")
print(var)
```

```
['Python', 'is', 'a', 'general', 'purpose', 'programming', 'language']
```

To see , How maxsplit work , i will be using them in below example:

In \[57]:

```
sentence = "Python is a general purpose programming language"
var = sentence.split(" ",2)
print(var)
```

```
['Python', 'is', 'a general purpose programming language']
```

rsplit() works like split() , it also splits the string from right to left. For Example:

In \[58]:

```
sentence = "Python is a general purpose programming language"
var = sentence.rsplit(" ")
print(var)
```

```
['Python', 'is', 'a', 'general', 'purpose', 'programming', 'language']
```

## Difference between split() and rsplit():

if you use split() and rsplit() method on a same string you will find there is no difference in output. let's use them both on a single string:

In \[59]:

```
sentence = "This is Console Flare Python class"
var = sentence.split(" ")
print(var)
```

```
['This', 'is', 'Console', 'Flare', 'Python', 'class']
```

```
sentence = "This is Console Flare Python class"
var = sentence.rsplit(" ")
print(var)
```

```
['This', 'is', 'Console', 'Flare', 'Python', 'class']
```

split() splits the string from left to right and rsplit() splits the string from right to left , yet output is same always.

Difference between split() and rsplit() can only be shown with maxsplit(). let's follow these examples:

In \[63]:

```
sentence = "This is Console Flare Python class"
var = sentence.split(" ",2)
print(var)
```

```
['This', 'is', 'Console Flare Python class']
```

In \[64]:

```
sentence = "This is Console Flare Python class"
var = sentence.split(" ",2)
print(var)
```

```
['This', 'is', 'Console Flare Python class']
```

Because rsplit() splits the string from right to left , maxsplit gives different result from split() method.

## 13.partition() and rpartition() method:

partition() method returns string where string is parted into three parts:

1.Everything before the first separator 2.Separator itself. 3.Everything after the first separator.

Parameter to be passed into method: separator

***partition(separator)***

In \[66]:

```
sentence = "python is Cool"
var = sentence.partition(" ")
print(var)
```

```
('python', ' ', 'is Cool')
```

![](https://767344992-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMiliykuSLaUcLgGbj1qR%2Fuploads%2FWjrcvKp4omBoNxY5SWPT%2Fimage.png?alt=media\&token=e1344a91-c0c9-45a0-af0b-081b326cac65)

**Note:**&#x72;partition() is same as partition() , only it starts from right to left.In \[67]:

```
sentence = "python is Cool"
var = sentence.rpartition(" ")
print(var)
```

```
('python is', ' ', 'Cool')
```

## Methods that returns only Boolean Values(True or False):

## 14.islower() method:

islower() returns True if all the characters in string is lowercase , otherwise it returns False. It has no Parameters.

In \[68]:

```
company = 'console flare'
var = company.islower()
print(var)
```

```
True
```

## 15.isupper() method:

isupper() returns True if all the characters in string is uppercase , otherwise it returns False. It has no Parameters.

In \[69]:

```
company = 'CONSOLE FLARE'
var = company.isupper()
print(var)
```

```
True
```

## 16.isalpha() method:

isalpha() returns True if all the characters in string is only alphabets , otherwise it returns False. It has no Parameters.

In \[70]:

```
company = 'CONSOLEFLARE'
var = company.isalpha()
print(var)
```

```
True
```

## 17.isnumeric() method:

isnumeric() returns True if all the characters in string is only numbers , otherwise it returns False. It has no Parameters.

In \[72]:

```
num = "15"
var = num.isnumeric()
print(var)
```

```
True
```

## 18.isalnum() method:

isalnum() returns True if all the characters in string is either number or string or both , otherwise it returns False. It has no Parameters.

In \[73]:

```
num = "123abc"
var = num.isalnum()
print(var)
```

```
True
```

## 19.startswith() method:

startswith() returns true if the string starts with a specified value.

Parameters to be Passed in method:

value : The values to check if the string starts with. It is **Required**

start : An integer specifying at which position to start the search. It is **Optional**

end : An integer specifying at which position to end the search. It is **Optional**

In \[74]:

```
name = "Console flare"
var = name.startswith("Con")
print(var)
```

```
True
```

## 20.endswith() method:

endswith() returns true if the string ends with a specified value.

Parameters to be Passed in method:

value : The values to check if the string ends with. It is **Required**

start : An integer specifying at which position to start the search. It is **Optional**

end : An integer specifying at which position to end the search. It is **Optional**

In \[75]:

```
name = "Console flare"
var = name.endswith("e")
print(var)
```

```
True
```

![](https://767344992-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMiliykuSLaUcLgGbj1qR%2Fuploads%2FHIKxIxa6xA9HTaFq0D7y%2Fimage.png?alt=media\&token=765b5a54-460b-4d99-94a9-0299441de62f)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://training.gitbook.io/python-documents/strings-in-python.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
