USING STRINGS IN PYTHON.

Biru
7 min readJul 13, 2020

Strings

Strings are the characters sequenced as a constant or variable. These strings are also called Array. It is called so, On the base of storing mechanism. They are store in memory as an array. The strings are surrounded by single or double quota i.e. “I am a string” or ‘I am a string’. let x and y be the variable and “I am a string” be its value then it can be written as the following script :

we can use type() function to check the variable type.It can be used as below:

After all, we are able to write a script using a string in a single-line format but what if we have to assign a multiline string to a variable.what if we have to write long sentences? for that question we can use the triple quotation marks as an answer. If you remember the triple quotation mark was used before in comment. we can use “””or ‘’’ to assign the value to the variable. for example, we take a and b as our variable where we have to save multiline of string. For this, we will see the following code.

They are for special purposes.

They are for special purposes.

Printing certain characters or words from the string or slicing.

Cutting some words from the words or sentences store in a variable that we want to print or want to do some scripting is slicing. It cuts the portion of the value that we tell it to cut. slicing gives a range of characters. It uses the starting index and end index which are separated by a semicolon to give the part of the string as output. Let see the script of slicing.

if you are wondering how the answer is m. Then read here, In the above, it is already mentioned that the strings are like an array on the base of storing mechanism. The string is store in memory as an array. In memory, they are store in a table with an index, and with the help of those indexes, we can access the element from the memory. For the above script, we can see below how the strings are store in a memory:

table: This table shows how the strings are store in the memory with an index.

When we give the Ok, that’s all right up to here but what if we want to print all the characters. The correct answer to print all is use print(x[3:5]) command for output. The command selected the index between 3 and 5 including 3 and excluding 5.i.e. the output will be m and space. if we give the command print(x[0:12]) the output willbe print(x[0:]) it will give the output I am a string.If we want to print only one character like t then we will give the command print(x[8]) then the output will be t. I am a strin.

with the help of positive index value, we can print the value but do we know we can print the value from negative indexing. Yes, we can also print the value from a negative index. When we give negative index value it counts the value from the end of the index or end of the string. It will take the last index value as 0 and 1 and goes on increasing to the end. Let’s take an above e.g. print(x[-5:-3]).It will print the value between 3 and 5 including 3 excluding 5. Furthermore, we can see below when it is negatively index. “I am a string” in this example the I was in 0 index and g was in 12 index and when you negatively index then I will be in 12 index and g will be in 0 negative index. When we will give the

table: This table shows how the strings are store in the memory with a negative index.

Below shows the script for the negative index and output.

Printing the length of the index or length of the string.

we have learned how to access the element using the index. But how will we know how many long the index is?. If the given string is short we can tell how long the index is but what if we have a long string and we want to use index. In that case, we can use the len() function. This function prints the length of the strings. Let see for above eg. “I am a string”.

If we see in the above table we will see there are 13 indexes starting from 0 and ending at 12 i.e. altogether 13.

Breaking strings to individual words or listing as list items.

split() function disintegrate the strings into a list as a list item. It will break all the strings in individual words. As an example, we can see below:

If we want to split the string from comma we can give the command as below.

txt = “hello, I am a string”

[‘hello’, ‘ i am a string’]

In the above output, we can see that in the output the string is breaked from comma. if we had given value as command as above “hello, I, am, a, string” and i.e x=txt.split(“,”) then the output would have been [‘hello’, ‘i’, ‘am’, ‘a’, ‘string’ ]. from this we can tell that in the command if we give the separate it will separate the words from the separator like in above comma was the separator. but if we don’t provide the separator it will use the default one i.e. whitespace.

Changing cases is easy in python there are six functions that are used.

This function capitalizes all the characters excluding the symbols and numbers. we can see below to see how to use upper():

txt = “hello, i am a string.”

y=”Hello!, I Am a String.(1)”

HELLO!, I AM A STRING.(1)

This function reverse of upper() function. lower() function does opposite of upper(). It converts all the characters into lowercase except symbols and numbers. As the above script can be seen below for using the lower() function.

txt = “HELLO, I AM A STRING.”

y=”Hello!, I Am a String.(1)”

hello!, i am a string.(1)

This function works as lower() function does. casefold() converts all the uppercase characters into lowercase. This can be seen below:

txt = “HELLO, I AM A STRING.”

y=”Hello!, I Am a String.(1)”

hello!, i am a string.(1)

title() function is similar to i.e the upper() changes all the characters to capital letters but the title() does not instead it changes the first letter of each word to capital letter and also there are words after the number and symbol, it will not exclude it but the letter after the number or symbol will be capitalized. Below script and output of title() function: upper() function. They both change the lowercase to upper but there is a slight and most important difference between them to be noticed

x = “hello, i am a string”

This function works as the title() function does but the difference is that it changes only one first letter of the character but it excludes the symbol and numbers. this can be seen below:

x = “hello, i am a string”

z=”1.hello, i am 1str#ing”

The swapcase() function converts all the uppercase to lowercase and vice-versa and it does not care about a non-alphanumeric character. This can be seen below:

x = “hello, i am a string”

z=”hello, i am 1str#ing”

c=”HELLO, I AM 1STR#ING”

In Python, strings can be found by using functions. There are two functions for finding the string one is find() and the second one is index(). They both are useful while finding the string.

This function is useful while finding the string. It returns the first occurrence of a string. It does not print the string rather it prints out the index number i.e. if we give the command to find the string that is occurred more than once then it will print the index of first appeared one. For e.g. x = “i am a string” then we give find() command to print i then it will print the index number i.e. 0 but it did not give the information about the i in last word string. Furthermore, we can see below:

find() also gives us a facility to find the characters between a certain index. Suppose we want to find the m between 0 and 5 index then we will give the following script.

note [if the occurrence of m were two between 0 and 5 then the output will have first occurred m]

It is same as find() function. index() index function works exactly as find() does. The script is for the index() is same as find() just replace the find() with index().

Note:[the only difference between find() and index() is that when find() doesn’t find the value it will give -1 as output and index will give exception this can be seen below:]

print(x.find(“h”)) #using find()

ValueError: substring not found

Replacing the string or using replace() function.

The replace function is used to erase the string and place another string in that place. Like in the paper we erase some words to replace those words with other words similarly we use a replace() function. using of replace function is given below:

Counting the number of repetitions of the words or using the count() function.

Reading the title the thing that strikes in our mind is to count the word. Yes, the function is used to count the number of times the particular word has occurred or occurred between certain positions and gives the repeated number as output. this can be seen below:

Concatenation or combining two or more strings:

Combining two string is done by + operator. This is similar to adding two numbers. Furthermore, we can see below:

Escape character or using backslash \”.

Sometimes situations arose we have to give the value inside the double or single quote and print in the same way for e.g x=”I am “a” string” in this eg a is inside the double quota and if we try to print the x. then it will throw an error or you can see below:

SyntaxError: invalid syntax

This problem can be solved by using Backslash \”.

Formating strings or using the format() function

This function returns the formatted strings. format() string is used to set the specific values inside the strings. The specific values are inserted inside a placeholder. Placeholders are defined using a curly bracket {}. Placeholder is identified using the name or numbered index also empty index are used.

Originally published at https://ilovecoode.blogspot.com on July 13, 2020.

--

--