<jupyter_start><jupyter_text>Python Strings- Strings is one of the most important data types.- Let's know how it is declared, defined, accessed and common operations performed on the python strings.  Strings- A string is a collection or series of characters.- An important point to remember is that python strings are **immutable**. Once,we created a string object, we cannot change them. - However, observe that python doesn't have character data type. It just treats a single character as a string of length one.- Anything enclosed in quotes(either single or double) is considered as a string.- Indexing in strings starts from 0.  Declaration Of Strings- The declaration of a string is very simple. It is similar to declaration as that of any other basic data type.- **Syntax**> *var_name = 'some text within quotes'* or *var_name = "some text within quotes"*- Lets look at an example!<jupyter_code>MyString = "Hello World!"<jupyter_output><empty_output><jupyter_text>Accessing Strings- Now, that we know how to initialise strings, lets look at how to access them.- We can use name of the variable to which the string is assigned, if we want to access the complete string.- To access only one character of a string ,we use indices.- To access a substring (a subsequence/part of total string), we use slicing.**Syntax**:> 1) *var_name* - to access the whole string.\  2) *var_name[index]* - to access a certain character at 'index' position.\  3) *var_name[index1:index2]* - to access a sequence of characters from index1 to index2-1 positions in the string.- Let's look at few examples of accessing strings.<jupyter_code>MyString = "I love GirlScript"
print(MyString)
print(MyString[0])
print(MyString[7:]) #if index2 is left empty it prints upto the end.<jupyter_output>I love GirlScript
I
GirlScript<jupyter_text>Operations On Strings Now, we had the basic knowledge of what string is, how they are declared and accessed . Let's see the operations that can be performed on strings.\ Some of the common operations are - length - Copy- Concatenation- Comparison- Finding if a substring exists in a string.These are some of the common operations performed on strings. Let's look at how these are performed. Finding length of string- The *len()* function in python gives us the length of string. - This can be done as follows:<jupyter_code>MyString = "I love GirlScript"
print("The length of MyString is "+ str(len(MyString))) # At this point, all you have to notice is that len(MyString) is giving the length of MyString variable.
# I'll get back to the part of str() and '+' later in this tutorial.<jupyter_output>The length of MyString is 17<jupyter_text>Copying Strings- There are many ways to copy strings in python. However, most of them creates a copy of the object but the new and old variables points same object.- Lets look into the ways how strings can be copied.1. Using **Slice operator**:- We can create a new variable and then use the actual variable that holds the string along with [ : ].- Let's look at an example.<jupyter_code>MyString = "I love GirlScript"
MyString2 = MyString[:]
print(MyString2)<jupyter_output>I love GirlScript<jupyter_text>2. Using **Assignment Operator**- We could simply use '=' to make a copy of string and now both the old and new variables refer same string object.- An example is as shown:<jupyter_code>MyString = "I love GirlScript"
MyString2 = MyString
print(MyString2)<jupyter_output>I love GirlScript<jupyter_text>3. Using **str()**- Now, lets learn about str() function in python.- str() converts any value within braces into string object and returns it.- Copying via str() can be done as shown:<jupyter_code>MyString = "I love GirlScript"
MyString2 = str(MyString)
print(MyString2)<jupyter_output>I love GirlScript<jupyter_text>4.We can even come up with our own program to copy strings. Here is one, to copy strings.<jupyter_code>MyString = "I love GirlScript"
MyString2 = '' # currently an empty string.
for i in MyString:
  MyString2 += i  #appending i to previous MyString2 and assigning new value to MyString. + is a way of cancatenation which we shall learn in detail in next section.
print(MyString2)<jupyter_output>I love GirlScript<jupyter_text>ConcatenationConcatenation is adding two or more strings and forming a larger string. This can be done using '+' operator.\An example is as shown:<jupyter_code>MyString = "I love GirlScript"
MyString2 = " Winter of Contribution"
MyString3 = MyString + MyString2
print(MyString3)<jupyter_output>I love GirlScript Winter of Contribution<jupyter_text>However, print() has its special of treating all its parameters.\Anything written in print() seperated by comma(,) is first converted to a string object and concatenated by adding a space in between. Lets see an example:<jupyter_code>print(MyString,MyString2)<jupyter_output>I love GirlScript  Winter of Contribution<jupyter_text>ComparisonString comparison is one of the most frequently used operations. \Strings can be considered just like basic data type such as int and comparison can be one using - ==-  &lt;-  &gt;-  &lt;=- &gt;=What these comparison operators generally does is, it checks the ascii value of each character until a mismatched value occurs, if there's no mismatch the strings are equal.<jupyter_code>print('a' > 'b') #output is false as ascii value of 'a' 97 is less than ascii value of 'b' 98
print('ab' == 'ab') #returns true as strings are same
print('ac' >= 'abd') #prints true as 'c'(99) is greater than 'b'(98)
print('ac' <= 'abd') #false as 'c' is greater than 'b'
print('ac' < 'abd') #result follows from above discussion<jupyter_output>False
True
True
False
False<jupyter_text>Finding a substring in a given stringfind() return the index at which substring occurs in given string and -1 if substring is not found.\find() takes in 3 parameters- str -> points substring-  beginning index -> specifies starting index from where search should start(this is 0 by default)- ending index -> This is the ending index, by default it is equal to the length of the string.Lets look at an example of how its done:<jupyter_code>MyString = "I love GirlScript"
Substr = "GirlScript"
print(MyString.find(Substr))<jupyter_output>7
{'fraction_non_alphanumeric': 0.07741935483870968, 'fraction_numerical': 0.007258064516129033, 'mean_word_length': 4.997098646034816, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '9297', 'license': 'MIT', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1713, 'n_words': 863, 'path': 'Python/Python_Strings/PythonStrings.ipynb', 'repo_name': 'SouravKr2001/winter-of-contributing', 'size': '6200'}