<jupyter_start><jupyter_text>Strings<jupyter_code>'''
A string is a sequence of characters
'''
fruit = 'banana'
letter = fruit[1]
print('fruit[]', letter)
mystring='apple'
type(mystring)
letter = fruit[0]
print('fruit[0]', letter)

# The value of the string index must be an integer. The following will give error
letter = fruit[1.5]
print(fruit[1.5], letter)
# getting length of string

fruit = 'banana'
length=len(fruit)
print('Length of fruit:', length)

# printing last letter of string

lastletter=fruit[length-1]
print('last letter of fruit:', lastletter)<jupyter_output>Length of fruit: 6
last letter of fruit: a<jupyter_text>Traverse through a string witk a loop<jupyter_code># One way to traverse through a string is to use while loop

print('using while loop')

index = 0
while index < len(fruit):
    letter =fruit[index]
    print(letter)
    index = index + 1
# alternative
print()
print('# using for loop')

for char in fruit:
    print(char)<jupyter_output># using for loop
b
a
n
a
n
a<jupyter_text>String slices<jupyter_code># printing selected ranges in  a string

s = '<NAME>'

print(s[0:5])#[start,n-1]
print(s[6:12])
# printing first three letters in a string

fruit = 'banana'

fruit[:3]


# printing letters 3rd onwards in a string
fruit[3:]
# if the fist index of string is greater than or equal to last index, resulting substring is empty
fruit= 'banana'
fruit[3:3]


# Exercise: What result will fruit[:] produce? try it

fruit[:]
# revers the string
fruit[::-1]<jupyter_output><empty_output><jupyter_text>Strings are immutable<jupyter_code>greetings ='Hello world!'

# Suppose we want to replace it by 'Jello world!'. 
# Trying to replace forst character by J would produce error because strings are immutable 

greetings[0] = 'J'
# Correct way to replace a part of string

new_greetings = 'J' + greetings[1:]# concatenate
print(new_greetings)
# Looping and counting
# How many 'a's are there in 'banana'

word = 'banana'
count = 0
for letter in word:
    if letter == 'a':
        count = count + 1
print(count)<jupyter_output>3<jupyter_text>The in operator<jupyter_code># We need to find out if the letter 'a' is in 'banana'
'a' in 'banana'   # this will return True
'seed' in 'banana' # will return False<jupyter_output><empty_output><jupyter_text>String comparisons<jupyter_code>if word == 'banana':
    print('All right, bananas.')
# Defining a function for checking words coming before or after banana
def banana(word):
    if word < 'banana':
        print('Your word, ' + word + ', comes before banana' )
    elif word > 'banana':
        print('Your word, ' + word + ', comes after banana' )
    else:
        print('Your word is banana')

banana('republic')
banana('American')
banana('Pineapple')

print()
        
print('Note: All large cap letters are greater than all small letters')    
#http://sticksandstones.kstrom.com/appen.html<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11578221915920055, 'fraction_numerical': 0.009648518263266712, 'mean_word_length': 4.714566929133858, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 24, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '253019', 'license': 'MIT', 'n_tokens_mistral': 1014, 'n_tokens_neox': 964, 'n_words': 336, 'path': 'Python Data Structure_ Srings.ipynb', 'repo_name': 'faisalnawazmir/Python_Lectures', 'size': '2908'}