Monthly Archives: September 2021

8.6 String Methods

Python has a set of built-in methods that you can use on strings. Note: All string methods returns new values. They do not change the original string. Example Upper case the first letter in this sentence:txt = “hello, and welcome to my world.”x = txt.capitalize()print (x) Output:Hello, and welcome to my world. Method Syntax Description capitalize() string.capitalize() Converts… Read More »

8.5 Escape Characters

To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \ followed by the character you want to insert. An example of an illegal character is a double quote inside a string that is surrounded by double quotes: Example You will get an error if you use double… Read More »

8.4 Format-Strings

As we learned in the Python Variables chapter, we cannot combine strings and numbers like this: Example age = 36txt = “My name is John, I am ” + ageprint(txt) Output:Traceback (most recent call last):  File “demo_string_format_error.py”, line 2, in <module>    txt = “My name is John, I am ” + ageTypeError: must be str, not int But we can… Read More »

8.3 String Concatenation

To concatenate, or combine, two strings you can use the + operator. Example Merge variable a with variable b into variable c:a = “Hello”b = “World”c = a + bprint(c) Output:HelloWorld Example To add a space between them, add a ” “:a = “Hello”b = “World”c = a + ” ” + bprint(c) Output:Hello World

8.2 Modify Strings

Python has a set of built-in methods that you can use on strings. Upper Case Example The upper() method returns the string in upper case:a = “Hello, World!”print(a.upper()) Output:HELLO, WORLD! Lower Case Example The lower() method returns the string in lower case:a = “Hello, World!”print(a.lower()) Output:hello, world! Remove Whitespace Whitespace is the space before and/or after the actual text, and very… Read More »

8.1 Slicing Strings

You can return a range of characters by using the slice syntax. Specify the start index and the end index, separated by a colon, to return a part of the string. Example Get the characters from position 2 to position 5 (not included):b = “Hello, World!”print(b[2:5]) Output:llo Note: The first character has index 0. Slice From the… Read More »

8. Strings

Strings in python are surrounded by either single quotation marks, or double quotation marks. ‘hello’ is the same as “hello”. You can display a string literal with the print() function: Example print(“Hello”)print(‘Hello’) Output:HelloHello Assign String to a Variable Assigning a string to a variable is done with the variable name followed by an equal sign and the string: Example… Read More »

7. Casting

Specify a Variable Type There may be times when you want to specify a type on to a variable. This can be done with casting. Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types. Casting in python is therefore done using constructor functions: int() – constructs… Read More »

6. Numbers

There are three numeric types in Python: int float complex Variables of numeric types are created when you assign a value to them: Example x = 1    # inty = 2.8  # floatz = 1j   # complex To verify the type of any object in Python, use the type() function: Example print(type(x))print(type(y))print(type(z)) Output:<class ‘int’><class ‘float’><class ‘complex’> Int Int, or integer, is a… Read More »

5. Data Types

Built-in Data Types In programming, data type is an important concept. Variables can store data of different types, and different types can do different things. Python has the following data types built-in by default, in these categories: Text Type: str Numeric Types: int, float, complex Sequence Types: list, tuple, range Mapping Type: dict Set Types: set, frozenset Boolean Type: bool… Read More »