Text And Lines

Text files are sequence of lines similar to Python string which is sequence of characters. For example let’s look following sequence of lines (downloaded from Wikipedia/Python):
Python is a widely used high-level programming language used for general-purpose programming, created by Guido van Rossum and first released in 1991. An interpreted language, Python has a design philosophy which emphasizes code readability (notably using whitespace indentation to delimit code blocks rather than curly braces or keywords), and a syntax which allows programmers to express concepts in fewer lines of code than possible in languages such as C++ or Java.[24][25] The language provides constructs intended to enable writing clear programs on both a small and large scale.[26]

Python features a dynamic type system and automatic memory management and supports multiple programming paradigms, including object-oriented, imperative, functional programming, and procedural styles. It has a large and comprehensive standard library.[27]

Python is widely used and interpreters are available for many operating systems, allowing Python code to run on a wide variety of systems. CPython, the reference implementation of Python, is open source software[28] and has a community-based development model, as do nearly all of its variant implementations. CPython is managed by the non-profit Python Software Foundation.
Copy the text and save it as P-files.txt
To break the file into lines, there is a special character that represents “end of the line” called the newline character. In Python, we represent the newline character as a backslash-n string constants. Even though this looks like two characters, it’s actually a single character.
First let’s define a following string in Python:
>>> word='Hello\nWorld!'
>>> word
'Hello\nWorld!'
>>> print word 
Hello
World!
First we’ve defined variable called world and assign string ‘Hello\nWorld!’, then we typed word and Python gave us the value of the variable word after we typed print word and Python recognized the “newline” character end printed out
Hello 
World!
If we want to check the length of a string use a built-in function len(word).
>>> len(word)
12
So length of word is 12 characters how’s that possible? Well ‘Hello’ has 5 characters and the ‘World!’ has 6 which totals 11 characters. This means that \n character is not 2 but a single character.

When we look at txt files we can imagine that there is a special invisible character at the end of each line that marks the end of the line called the newline. So the newline character separates the characters in the file into lines. 

Nema komentara:

Objavi komentar