<jupyter_start><jupyter_text>Python Strings String Operations Welcome! This notebook will teach you about the string operations in the Python Programming Language. By the end of this notebook, you'll know the basics string operations in Python, including indexing, escape sequences and operations.  Table of Contents                        What are Strings?                            Indexing                            Negative Indexing                Slicing                Stride                Concatenate Strings                                        Escape Sequences                            String Operations                            Quiz on Strings                        Estimated time needed: 15 min     What are Strings? The following example shows a string contained within 2 quotation marks:<jupyter_code># Use quotation marks for defining string

"<NAME>"<jupyter_output><empty_output><jupyter_text>We can also use single quotation marks:<jupyter_code># Use single quotation marks for defining string

'<NAME>'<jupyter_output><empty_output><jupyter_text>A string can be a combination of spaces and digits:<jupyter_code># Digitals and spaces in string

'1 2 3 4 5 6 '<jupyter_output><empty_output><jupyter_text>A string can also be a combination of special characters :<jupyter_code># Special characters in string

'@#2_#]&*^%$'<jupyter_output><empty_output><jupyter_text>We can print our string using the print statement:<jupyter_code># Print the string

print("hello!")
Ne = input("Enter your Name")  # by Default input Funcation will Consider it as a String
print(Ne,type(Ne))<jupyter_output>Enter your Name Reddy<jupyter_text>We can bind or assign a string to another variable:<jupyter_code># Assign string to variable

Name = "<NAME>";
Name
len(Name)
ord('M') # To find the Char number in ASCII or UNICODE
ord('m')
chr(65) # it used for Find the Order of a Char<jupyter_output><empty_output><jupyter_text>Indexing It is helpful to think of a string as an ordered sequence. Each element in the sequence can be accessed using an index represented by the array of numbers:     The first index can be accessed as follows: [Tip]: Because indexing starts at 0, it means the first index is on the index 0.<jupyter_code># Print the first element in the string

print(Name[0])<jupyter_output>M<jupyter_text>We can access index 6:<jupyter_code># Print the element on index 6 in the string

print(Name[6])<jupyter_output>l<jupyter_text>Moreover, we can access the 13th index:<jupyter_code># Print the element on the 13th index in the string

print(Name[13])<jupyter_output>o<jupyter_text>Negative Indexing  We can also use negative indexing with strings:  Negative index can help us to count the element from the end of the string. The last element is given by the index -1:<jupyter_code># Print the last element in the string

print(Name[-1])<jupyter_output>n<jupyter_text>The first element can be obtained by  index -15:<jupyter_code># Print the first element in the string

print(Name[-15])
Name[-16]<jupyter_output><empty_output><jupyter_text>We can find the number of characters in a string by using len, short for length:<jupyter_code># Find the length of string

len("<NAME>")<jupyter_output><empty_output><jupyter_text>Slicing We can obtain multiple characters from a string using slicing, we can obtain the 0 to 4th and 8th to the 12th element:               Variable_Name[Start : end-1] or              Variable_Name[Start : len-1]  [Tip]: When taking the slice, the first number means the index (start at 0), and the second number means the length from the index to the last element you want (start at 1)Slice or indexing Has to Start from Left to Right only<jupyter_code># Take the slice on variable Name with only postive index 0 to index 3

Name[0:4]
# Take the slice on variable Name with only postive index 8 to index 11

Name[8:12]
# Take the slice on variable Name with only nagative index -15 to index -11
Name[-15:-11]
# Take the slice on variable Name with only nagative index -7 to index -2
Name[-7:-2]
Name[-15:]
Name[:]
Name[:15]
Name[-1:0]<jupyter_output><empty_output><jupyter_text>Stride or Extendend Slice  We can also  input a stride value as follows, with the '2' indicating that we are selecting every second variable: $Varable Name [Start:legnth-1:Step]$<jupyter_code># Get every second element. The elments on index 1, 3, 5 ...

Name[::2]
Name[::3]
Name[::4]
# String in a Reverser Order
Name[::-1]
Name[::14]
Name[12::] # By default Step is 1
Name[-3::]
# Get every second element in the range from index 0 to index 4

Name[0:5:2]<jupyter_output><empty_output><jupyter_text>Concatenate Strings We can concatenate or combine strings by using the addition symbols, and the result is a new string that is a combination of both:<jupyter_code># Concatenate two strings

Statement = Name + " is the best"
Statement
Name[:7]+" is Best in dance"<jupyter_output><empty_output><jupyter_text>To replicate values of a string we simply multiply the string by the number of times we would like to replicate it. In this case, the number is three. The result is a new string, and this new string consists of three copies of the original string:<jupyter_code># Print the string for 3 times

3 * "<NAME>"<jupyter_output><empty_output><jupyter_text>You can create a new string by setting it to the original variable. Concatenated  with a new string, the result is a new string that changes from <NAME> to “<NAME> the best".<jupyter_code># Concatenate strings

Name = "<NAME>"
Name = Name + " is the best"
Name*3<jupyter_output><empty_output><jupyter_text>Escape Sequences Back slashes represent the beginning  of escape sequences. Escape sequences represent strings that may be difficult to input. For example, back slash "n" represents a new line. The output is given by a new line after the back slash "n" is encountered:<jupyter_code># New line escape sequence

print(" <NAME> \n is the best" )<jupyter_output><empty_output><jupyter_text>Similarly, back slash  "t" represents a tab:<jupyter_code># Tab escape sequence
print(" <NAME> \t is the best" )
a = "Hello\t world"
a.expandtabs(20)
# Vertical Tab escape sequence

print(" <NAME> \v is the best" )
ord('\x48'),ord('\x45')
chr(72),chr(69)
# Hex Decimal Values 

print("This is \x48\x45\x58 representation")<jupyter_output><empty_output><jupyter_text>If you want to place a back slash in your string, use a double back slash:<jupyter_code># Include back slash in string

print(" <NAME> \\ is the best" )<jupyter_output><empty_output><jupyter_text>We can also place an "r" before the string to display the backslash:<jupyter_code># r will tell python that string will be display as raw string

print(r" <NAME> \ is the best" )
print(" <NAME> \' is the best" )
print(" <NAME> \" is the best" )
"Hello world Welocome to python Class's"
'Hello world Welocome to python Class's'
pi = 3.14
print("Pi value in math is ",pi)
print("Pi value is: " + str(pi))
str(pi)
Name = "Hello Welcome to Python Class's"
print(Name)
Name = 'Hello Welcome to Python Class's'<jupyter_output><empty_output><jupyter_text>Input ()`input` is Built in Funcation which will take Input as **String** by Default<jupyter_code>a = input("Enter the Data:")    # STDIN(SYSTEM INPUT)   
print(a,'\n',type(a))                        # STDOUT(SYSTEM OUTPUT)
b = int(input("Enter the Interger Data:"))
print(b)
type(b)
print(float(b))
a = input()
type(a)
int(a),float(a),complex(a)
a,b = map(int,input("Enter the Data:").split(' '))
print(a,b)
a
b
"Hello World Welcome to python Class".split(' ')<jupyter_output><empty_output><jupyter_text>String Operations There are many string operation methods in Python that can be used to manipulate the data. We are going to use some basic string operations on the data.  Let's try with the method upper; this method converts lower case characters to upper case characters:<jupyter_code># Convert all the characters in string to upper case

A = "Thriller is the sixth Studio Album"
print("before upper:", A)
B = A.upper()
print("After upper:", B)
"before upper: Thriller is the s\txth Studio Album".upper()<jupyter_output><empty_output><jupyter_text>The method replace replaces a segment of the string, i.e. a substring  with a new string. We input the part of the string we would like to change. The second argument is what we would like to exchange the segment with, and the result is a new string with the segment changed:<jupyter_code># Replace the old substring with the new target substring is the segment has been found in the string

A = "<NAME> is the best Michael"
B = A.replace('Michael', 'Mike')
B
a = "Python"
a.replace('P','c')<jupyter_output><empty_output><jupyter_text>The method find finds a sub-string. The argument is the substring you would like to find, and the output is the first index of the sequence. We can find the sub-string jack or el.<jupyter_code># Find the substring in the string. Only the index of the first elment of substring in string will be the output

Name = "<NAME>"
Name.find('el')
# Find the substring in the string.

Name.find('Jack')<jupyter_output><empty_output><jupyter_text>If the  sub-string is not in the string then the output is a negative one. For example, the string 'Jasdfasdasdf' is not a substring:<jupyter_code># If cannot find the substring in the string

Name.find('Jasdfasdasdf')
A = "Hello \t world"
A.expandtabs(50)
A.count('l')
'name'.capitalize()
Name
Name.startswith('m')
Name.startswith('M')
Name.endswith('n')
Name.endswith('N')
Name.index('M')
"Hello".index('lo')
e = input("Enter the Name:")
'Hi {}'.format(e)
print("The {} Best Singer Name is {} ".format(A,Name))
print("The Left Alignment is |{0:<6}| and Center Alignement is |{1:^6}| and Right Alignment is |{2:>6}|".format(10,10,10))
"The Binary Number is |{0:b}| and Oct is |{0:o}| and Hex is |{2:x}|".format(2,10,10)
'{0!s} {0!r}'.format("Hello world")
'{:10}'.format("test")
'{:<10}'.format("test")
'{:_<10}'.format("test")
'{:_^9}'.format("ZIP")
'%.6s' %('xylophone')  # old Techniq
'{:.2}'.format('xylophone') # new Techniq<jupyter_output><empty_output><jupyter_text>Numbers For Formating<jupyter_code>'{:d}'.format(42)
'{:f}'.format(3.25)
'{:3d}'.format(42)
'{:07.3f}'.format(3.25)
Name.lower()
Name.center(25)
Name.split(' ')
fill = '*'
size = 20
Name.ljust(size,fill)
Name.zfill(20)
'Hello'.isalpha()
'123'.isalpha()
"Hello123".isdigit()
'123'.isdigit()
"123hello".isalnum()
'123'.isnumeric()<jupyter_output><empty_output><jupyter_text>Quiz on Strings What is the value of the variable A after the following code is executed?<jupyter_code># Write your code below and press Shift+Enter to execute<jupyter_output><empty_output><jupyter_text>Double-click here for the solution.<!-- Your answer is below:"1"--> What is the value of the variable B after the following code is executed?<jupyter_code># Write your code below and press Shift+Enter to execute<jupyter_output><empty_output><jupyter_text>Double-click here for the solution.<!-- Your answer is below:"2"--> What is the value of the variable C after the following code is executed?<jupyter_code># Write your code below and press Shift+Enter to execute<jupyter_output><empty_output><jupyter_text>Double-click here for the solution.<!-- Your answer is below:"12"-->  Consider the variable D = "HEllO" use slicing to print out the first three elements:<jupyter_code># Write your code below and press Shift+Enter to execute<jupyter_output><empty_output><jupyter_text>Double-click here for the solution.<!-- Your answer is below:print(D[:3])  or print(D[0:3])-->  Use a stride value of 2 to print out every second character of the string E = "Hello World":<jupyter_code># Write your code below and press Shift+Enter to execute<jupyter_output><empty_output><jupyter_text>Double-click here for the solution.<!-- Your answer is below:print(E[::2])-->  Print out a backslash:<jupyter_code># Write your code below and press Shift+Enter to execute<jupyter_output><empty_output><jupyter_text>Double-click here for the solution.<!-- Your answer is below:print("\\")orprint(r" \ ")-->  Find the `You` how many time Repreted in the Given String Data = "The World is Looking for you \n to update your Skils"<jupyter_code># Write your code below and press Shift+Enter to execute<jupyter_output><empty_output><jupyter_text>Convert the variable F = "hello" to uppercase:<jupyter_code># Write your code below and press Shift+Enter to execute<jupyter_output><empty_output><jupyter_text>Double-click here for the solution.<!-- Your answer is below:F.upper()-->  Consider the variable G, and find the first index of the sub-string snow:<jupyter_code># Write your code below and press Shift+Enter to execute<jupyter_output><empty_output><jupyter_text>Double-click __here__ for the solution.<!-- Your answer is below:G.find("snow")--> In the variable G, replace the sub-string Mary with Bob:<jupyter_code># Write your code below and press Shift+Enter to execute<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10521026592455164, 'fraction_numerical': 0.01461038961038961, 'mean_word_length': 4.937127122533272, 'pattern_counts': {'":': 1, '<': 172, '<?xml version=': 0, '>': 170, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '37', 'dir': 'jupyter-structured-clean-dedup', 'id': '137134', 'license': 'MIT', 'n_tokens_mistral': 4199, 'n_tokens_neox': 4030, 'n_words': 1584, 'path': 'Strings/PY0101EN-1-2-Strings.ipynb', 'repo_name': 'reddyprasade/PYTHON-BASIC-FOR-ALL', 'size': '13092'}