Python-ликбез. Модуль #1
Урок 1: План курса. Строки.
Учим Strings, методы, и много других страшных слов.
Python-Ликбез. Модуль №1
Урок №1
Accessing Values in Strings
Python does not support a character type, these are treated as strings of length one, also considered as substring.

We use square brackets for slicing along with the index or indices to obtain a substring.

var1 = "Guru99!"
var2 = "Software Testing"
print ("var1[0]:",var1[0])
print ("var2[1:5]:",var2[1:5])

Output:

var1[0]: G
var2[1:5]: oftw

Some more examples
You can update Python String by re-assigning a variable to another string. The new value can be related to previous value or to a completely different string all together.

x = "Hello World!"
print(x[:6])
print(x[0:6] + "Guru99")


Output:
Hello
Hello Guru99


Note : - Slice:6 or 0:6 has the same effect

Python String replace() Method
The method replace() returns a copy of the string in which the values of old string have been replaced with the new value.

oldstring = 'I like Guru99'
newstring = oldstring.replace('like', 'love')
print(newstring)


Output
I love Guru99
Changing upper and lower case strings
Changing upper and lower case strings

In Python, you can even change the string to upper case or lower case.

string="python at guru99"
print(string.upper())


Output
PYTHON AT GURU99

Likewise, you can also do for other function as well like capitalize
string="python at guru99"
print(string.capitalize())


Output
Python at guru99

You can also convert your string to lower case
string="PYTHON AT GURU99"
print(string.lower())


Output
python at guru99
Using "join" function for the string
The join function is a more flexible way for concatenating string. With join function, you can add any character into the string.

For example, if you want to add a colon (:) after every character in the string "Python" you can use the following code.

print(":".join("Python"))

Output
P:y:t:h:o:n
Reversing String
By using the reverse function, you can reverse the string. For example, if we have string "12345" and then if you apply the code for the reverse function as shown below.

string="12345"
print(''.join(reversed(string)))

Output
54321
Split Strings
Split strings is another function that can be applied in Python let see for string "guru99 career guru99". First here we will split the string by using the command word.split and get the result.

word="guru99 career guru99"
print(word.split(' '))


Output
['guru99', 'career', 'guru99']

To understand this better we will see one more example of split, instead of space (' ') we will replace it with ('r') and it will split the string wherever 'r' is mentioned in the string

word="guru99 career guru99"
print(word.split('r'))

Output
['gu', 'u99 ca', 'ee', ' gu', 'u99']

Important Note:
In Python, Strings are immutable.
Consider the following code

x = "Guru99"
x.replace("Guru99","Python")
print(x)


Output
Guru99

will still return Guru99. This is because x.replace("Guru99","Python") returns a copy of X with replacements made.

You will need to use the following code to observe changes

x = "Guru99"
x = x.replace("Guru99","Python")
print(x)


Output
Python
Summary
Since Python is an object-oriented programming language, many functions can be applied to Python objects. A notable feature of Python is its indenting source statements to make the code easier to read.

  • Accessing values through slicing - square brackets are used for slicing along with the index or indices to obtain a substring.
    • In slicing, if range is declared [1:5], it can actually fetch the value from range [1:4]
  • You can update Python String by re-assigning a variable to another string
  • Method replace() returns a copy of the string in which the occurrence of old is replaced with new.
    • Syntax for method replace: oldstring.replace("value to change","value to be replaced")
  • String operators like [], [ : ], in, Not in, etc. can be applied to concatenate the string, fetching or inserting specific characters into the string, or to check whether certain character exist in the string
  • Other string operations include
    • Changing upper and lower case
    • Join function to glue any character into the string
    • Reversing string
    • Split string
Отличная работа!
Есть вопросы? Задать их можно тут!
Форма с вопросами
Понравился урок?
Made on
Tilda