<jupyter_start><jupyter_text>Strings<jupyter_code>mystring = "Hello"
type(mystring)
mystring
print(mystring)
# built in Unicode Support
myfriend = "Jānis"
mycat = "Ņauva"
print(myfriend,mycat)<jupyter_output><NAME><jupyter_text>Strings* immutable* Unicode support* implement all common sequence operatorshttps://docs.python.org/3/library/stdtypes.htmltypesseq-common* string specific methodshttps://docs.python.org/3/library/stdtypes.htmlstring-methods<jupyter_code>name = "Valdis"
print(name)
len(name)
name = "Voldemars"
len(name)
len("name")
len("Valdis")
# first character in your string
name[0],name[1],name[2]
"Valdis"[0]
name[2]
name[5]
name = "Valdis"
len(name)
name[5]
#not a good pattern! anti-pattern a bad code smell
name[len(name)-1]
name[-1]
name[-2], name[4]
len(name)
name[5],name[-1],name[-2]
name[-1]
name[-2]
name[-6]
name[6]
empty = ""
empty[0]
# Slicing syntax
# Start at 0 an go until but not including 3
# some languages would do with name.subslice(0,3)
name[0:3]
# begginning 0 is not necessary
name[:3]
name[:4]
name[1:3] # so 2nd and 3rd(with index 2) but not the 4th(with index 3)
name[1:4]
name[-3], name[-3:-1], name[-3:]
# so we can use single index for single characters
# [start:beg] for range of characters
# [starg:beg:step] for jumping over characters
name[0:6:2], name[:6:2],name[::2]

name, name[0:6:1], name[::]

food = "potatoes"
food[::2]
food[1::2]
food[1:6:2]
name[::1]
name[1:6:2]
name[:5]
# Pythonic way of reersing a string
name[::-1]
food[::-1]<jupyter_output><empty_output><jupyter_text>String Exercise: * Assign your favorite food to variable of your choosing* Output 3rd letter of your favorite* Output first 2 letters of your favorite* Output last 3 letters of your favorite* Output reverse of your favorite* Output every 2nd character of your favorite in reverse<jupyter_code>myfood = "ice cream"
myfood[2]
myfood[:2], myfood[-3:]
myfood[::-1],myfood[::-2]
food[4::-1]
name[5:2:-1]
# Remember stings are immutable, so how do we change them
name[3]='b'
#name[3]="b" # Can we do this?
newname=name[:3]+"z"+name[4:]
print(newname)
#name[3]="b" # Can we do this?
newname=name[:3]+"z"+name[3:]
print(newname)
# so an answer is string concatenation with +
mycat = "kaķis" + " Muris"
mycat

len(mycat)
n=name
n2=name
print(n2)
print(id(n2),id(name), id(n))
print(name[:])
name == n2
name == newname[:3] + 'd' + newname[4:]
newname[:3] + 'd' + newname[4:]
name = "Voldemars"
print(n,n2,name)
n2 = "Something"
name=name[::-1]
print(name)
newline_string ="firstline\nsecondline"
print(newline_string)
newline_string
# in Python single and double quotes work the same way, you just need a matching pair
quote = "Alice told the rabbit 'go down the hole' and rabbit complied"
print(quote)
quote = "Alice told the rabbit \"go down the hole\" and rabbit complied"
print(quote)
quote = 'Alice told the rabbit "go down the hole" and rabbit complied'
print(quote)
multi_string = """Really
Long
And
Boring
    With Taba and maybe 'quote something' and even use double quotes "for talking" and so on \"\"\"
String"""
print(multi_string)
mytext = 'Just a quote'
mytext
myquote = 'They said "It was a long and stormy night" ending the story'
myquote
myquote = "Another quote\"Stormy night\" continuing the story"
myquote
"Valdis" + 5
# the answer is casting number to string beforehand
"Valdis " + str(5),"Valdis " + '5'
a=10
t="Talk "+str(a)
print(t)
t = "Talk "+"10"
print(t)
str(555)
id('25'),id(25)
'25'+'45',25+45
int('345')
int("70")
int('70')
?str
much_beer = "Beer"*5 # i can multiple strings this could come in handy in homework #1
much_beer
# Automatic string concatenation without +
funny_string = ('Monty '
'Python ' 
'Spam '*5)
print(funny_string)
# Notice that string multiplication happens after concatenation
print("It's \ta \"wonderful\" \nlife")
# How to print a line which does not fit in a single line
print('Really really long line\
   mVW')
print('Alice said "hello"')<jupyter_output>Alice said "hello"<jupyter_text>Fstrings (new since Python 3.6)* faster way to format strings* can use variables in current scope* same idea as .format() but faster and easier to read<jupyter_code>newstr = f"2+2={2+2} nice isn't {name[::-1]*3}"
newstr
# actual real usage
weekday="Tuesday"
month="October"
day=13
greeting = f"Today is {day}th of {month} which is {weekday}. Nice {month} isn't it ? "
print(greeting)
#nicer than format method
# notice how I did not have to cast 13 to str(13) in the above example
seconds=345.24556
width=32 # how much space our formatted value should take up
precision=4 # how many digits to use
print(f'Elapsed {seconds} seconds since start of new era')
print(f'Elapsed {seconds:{width}.{precision}} seconds since start of new era')
print(f'Elapsed {seconds:{9}.{6}} seconds since start of new era')
print(f'Number {75556.2222:{12}.{8}}')
myresult = f"0.1+0.2={0.1+0.2:{4}.{4}}"
myresult
greeting[10:33:1] # you can always get a slice from a string
my_hello = f"Hello {name}, nice day isn't day for a walk?"
print(my_hello)<jupyter_output>Hello Valdis, nice day isn't day for a walk?<jupyter_text>Python 3.X strings are Unicode by default no more 'u prefixes<jupyter_code>myfood = "porridge"
myfood
myfood[2]
myfood[:2]
myfood[-3:]
myfood[::-1]
myfood[::-2]
bigfood = myfood.capitalize() # i pressed tab after . and saw all kind of methods I can apply to my string
bigfood
myfood.capitalize() # note that capitilize DOES NOT change the string, it makes a new one
# count the number of occurences in a string
myfood.count('r')
# count the number of occurences in a string
myfood.count('idg')
myfood.count('a')
# if you just want to check whether there are any specific characters in your string
'u' in myfood
'o' in myfood
'C' in myfood
# we can check for substring in our string
'rid' in myfood
# we can check for substring in our string
'put' in bigfood
'somethinglongthatisnot' in myfood
# find first index of where the substring starts in our string
# remember we count from 0!
myfood.index('rid')
myfood.find('notfound') # so find gives you -1 if not found while index gives you error if not found
myfood.find('rid')
myfood[myfood.index('rid'):] # so find where rid starts and give me the rest of the string
myfood.upper()
name[0:6:1]

name.lower()
name.isdecimal()
"55555".isdecimal()
# ourstring. then use Tab to see what functions/methods are available
# methods/functions use parenthesis "Valdis".upper()<jupyter_output><empty_output><jupyter_text>What about looping through our strings?<jupyter_code>for c in name:
    print(c)
    print(c.upper())
    print(f"Letter {c=}") # this is new to python 3.8+ so will not work on python 3.7
print(f"My variable {name=}")
# tiny practice 
# print only letters in your name less than "i" and make them uppercase
limit = "i"
result = ""
print(f"Our {limit=} code is {ord(limit)}")
for c in name:
    print(f"{c=} code is {ord(c)}")
    if c.lower() < limit:
        print(c)
        result += c 
        # it is same as saying result = result + c, we make a new string overwriting the old one
        # for very long strings this could get pretty slow ( i am talking huuuge strings)
print(result)
result.upper()
mycity = "    Riga \t\n "
mycity
clean_city = mycity.strip() # so whitespace are any non character symbols
clean_city
myfood = "chocolate ice cream sunday"
myfood
myfood.capitalize(), myfood.title()
min(name)
max(name) #the character with the highest ASCII value in the string
min(myfood)
max(myfood)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13336013939150249, 'fraction_numerical': 0.02720814904168342, 'mean_word_length': 5.223519599666389, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 23, 'https://': 2, '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': '338787', 'license': 'MIT', 'n_tokens_mistral': 2731, 'n_tokens_neox': 2621, 'n_words': 921, 'path': 'core/Python Strings.ipynb', 'repo_name': 'ZaneKrumina/https-github.com-ValRCS-RTU_Python_720_Fall_2020-', 'size': '7466'}