Python strings

Share me please

Python string concatenating

# two string variable
str1 = "any text 1"
str2 = "any text 2"
# join two strings = concatenat
str3 = str1 + " " + str2
print(str3)

Getting access to string’s characters

# get concrete letter from any string
str1 = "Lorem ipsum lorem ipsum"
# get the 1 i 3 letter
print(str1[0] )
print(str1[2])

Python strings negative vs positive indexing

str1 = "Simple word"
print("S | i | m | p | l | e |   | w | o | r |  d ")
print("0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 ")
print(str1[0])
print(str1[10])
print(str1[1:6])
print(str1[0:11])
print("------------------------------------------")
print(" S  | i   |  m |  p |  l |  e |    |  w |  o |  r |  d ")
print("-11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 ")
print(str1[-1])
print(str1[-11])
print(str1[-10])
print(str1[:-5])
print(str1[-5:-1])

Strings stride omit some letters

str1 = "Simple word"
print("S | i | m | p | l | e |   | w | o | r |  d ")
print("0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 ")
 
# print all letters in str1 string
print(str1)
 
# print all letters in str1 string
print(str1[0:11])
 
# again print all letters in str1 string
print(str1[0:11:1])
 
# print every second letter in str1 string
print(str1[0:11:2])
 
# print every third letter in str1 string starting from the 4th position
print(str1[4:11:3])

Python functions to play with strings

str1 = "Simple word is just a word"
print("S | i | m | p | l | e |   | w | o | r |  d |    |  i |  s |    |  j |  u |  s |  t |    |  a |    | w  |  o | r  | d ")
print("0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 ")
 
# count letters in the str1
print( len(str1) )
 
# find the position where the word "word" starts in the str1 string
print( str1.find("word"))
 
# count how many words "word" are in the string str1
print( str1.count("word"))
 
# make the letters lower
print( str1.lower() )
 
# make the letters upper
print( str1.upper() )
 
# replace some part of the string
print( str1.replace("word", "banana") )