How to lowercase first letter of each new line in python? - python-3.x

myString = "I Am New To Python,
Trying to learn Different things.
Need your help in this Case."
I want like this:
myString2 = "i Am New To Python,
trying to learn Different things.
need your help in this Case."
How can I do this?

A regular expression substitution matching the first character in a line and replaced with its lower-case version is pretty straightforward:
import re
myString = '''I Am New To Python,
Trying to learn Different things.
Need your help in this Case.'''
print(re.sub(r'^(\s*.)',lambda m: m.group(1).lower(),myString,flags=re.MULTILINE))
Output:
i Am New To Python,
trying to learn Different things.
need your help in this Case.
Note you need to triple-quote your string to be valid. I included the leading white space in the replacement so the result would remove it. Use r'^\s*(.)' to leave it in.
The replacement is an anonymous function that receives the match object of the regular expression.

Using list comprehension you can do like,
myString = '''I Am New To Python,
Trying to learn Different things.
Need your help in this Case.'''
modified_string = '\n'.join([i.strip()[0].lower()+i.strip()[1:] for i in myString.split('\n')])
Hope this helps! Cheers!

If you don't know list comprehension yet, this might be an easier one to understand for beginners.
myString = """I Am New To Python,
Trying to learn Different things.
Need your help in this Case."""
mystring1=myString.splitlines()
mystring2=""
for x in mystring1:
mystring2 = mystring2 + x[0].lower() + x[1:] +"\n"
print(mystring2)

Related

Write a Python program that replaces all periods in a paragraph with "\n"

Is there anybody who can get me started on this? I have very very few knowledge about python and at my college python is not even taught. However, in my Linux class my instructor insists in writing a program that replaces all periods within a paragraph with a new line. I do not want you to make my homework, but I feel a bit lost with this assignment. My pre knowledge is in c and java, both introduction levels. Can you please give me a hint on even how to start on this? Thank you very much.
Python has a string method called replace. You can use it like this.
string = """this is a test. This is another. And so on."""
newstring = string.replace(".", "\n")
print(newstring)
You can try yourself here.

LibreOffice Basic: existing utilities for splitting strings?

I'm using the LibreOffice Basic language.
I'm wondering if there is any library anywhere I can use for splitting strings into arrays? For example, suppose I have the following string with items separated by an arbitrary number of spaces:
ABC DEF GHI
I'd like to split this string into an array called "item" with the following elements:
item(0) = "ABC"
item(1) = "DEF"
item(2) = "GHI"
I know how to produce these results in LibreOffice Basic using regular expressions or via iterating character-by-character through the original string, but I'm wondering if there are any existing functions or helper utilities I can use, so I don't have to "re-invent the wheel".
Internet searches have not yielded anything, but I could possibly have overlooked something.
Thank you in advance.
It looks like you will need to write your own function. There are several ideas at https://forum.openoffice.org/en/forum/viewtopic.php?f=9&t=33218.
If you will be doing a lot of string manipulation and the project is not too far along yet, then it might be worth considering another UNO-enabled language like Java or Python. In Python the code would be simply:
s = "ABC DEF GHI"
item = s.split()

How to compare Strings and put it into another program?

i´ve got small problem and before I spend even more time in trying to solve it i´d like to know if what I want to do is even possible ( and maybe input on how to do it^^).
My problem:
I want to take some text and then split it into different strings at every whitespace (for example "Hello my name is whatever" into "Hello" "my" "name" "is" "whatever").
Then I want to set every string with it´s own variable so that I get something alike to a= "Hello" b= "my" and so on. Then I want to compare the strings with other strings (the idea is to get addresses from applications without having to search through them so I thought I could copy a telephone book to define names and so on) and set matching input to variables like Firstname , LastName and street.
Then, and here comes the "I´d like to know if it´s possible" part I want it to put it into our database, this means I want it to copy the string into a text field and then to go to the next field via tab. I´ve done something like this before with AutoIT but i´ve got no idea how to tell AutoIT whats inside the strings so I guess it must be done through the programm itself.
I´ve got a little bit of experience with c++, python and BATCH files so it would be nice if anyone could tell me if this can even be done using those languages (and I fear C++ can do it and I´m just to stupid to do so).
Thanks in advance.
Splitting a string is very simple, there is usually a built in method called .split() which will help you, the method varies from language to language.
When you've done a split, it will be assigned to an array, you can then use an index to get the variables, for example you'd have:
var str = "Hello, my name is Bob";
var split = str.split(" ");
print split[0]; // is "Hello,"
print split[1]; // is "my" etc
You can also use JSON to return data so you could have an output like
print split["LastName"];
What you're asking for is defiantly possible.
Some links that could be useful:
Split a string in C++?
https://code.google.com/p/cpp-json/

What's the point of nesting brackets in Lua?

I'm currently teaching myself Lua for iOS game development, since I've heard lots of very good things about it. I'm really impressed by the level of documentation there is for the language, which makes learning it that much easier.
My problem is that I've found a Lua concept that nobody seems to have a "beginner's" explanation for: nested brackets for quotes. For example, I was taught that long strings with escaped single and double quotes like the following:
string_1 = "This is an \"escaped\" word and \"here\'s\" another."
could also be written without the overall surrounding quotes. Instead one would simply replace them with double brackets, like the following:
string_2 = [[This is an "escaped" word and "here's" another.]]
Those both make complete sense to me. But I can also write the string_2 line with "nested brackets," which include equal signs between both sets of the double brackets, as follows:
string_3 = [===[This is an "escaped" word and "here's" another.]===]
My question is simple. What is the point of the syntax used in string_3? It gives the same result as string_1 and string_2 when given as an an input for print(), so I don't understand why nested brackets even exist. Can somebody please help a noob (me) gain some perspective?
It would be used if your string contains a substring that is equal to the delimiter. For example, the following would be invalid:
string_2 = [[This is an "escaped" word, the characters ]].]]
Therefore, in order for it to work as expected, you would need to use a different string delimiter, like in the following:
string_3 = [===[This is an "escaped" word, the characters ]].]===]
I think it's safe to say that not a lot of string literals contain the substring ]], in which case there may never be a reason to use the above syntax.
It helps to, well, nest them:
print [==[malucart[[bbbb]]]bbbb]==]
Will print:
malucart[[bbbb]]]bbbb
But if that's not useful enough, you can use them to put whole programs in a string:
loadstring([===[print "o m g"]===])()
Will print:
o m g
I personally use them for my static/dynamic library implementation. In the case you don't know if the program has a closing bracket with the same amount of =s, you should determine it with something like this:
local c = 0
while contains(prog, "]" .. string.rep("=", c) .. "]") do
c = c + 1
end
-- do stuff

Replacing or substituting in a python string does not work

I could almost solve all of my python problems thanks to this great site, however, now I'm on a point where I need some more and specific help.
I have a string fetched from a database which looks like this:
u'\t\t\tcase <<<compute_type>>>:\n\t\t\t\t{\n\t\t\t\t\tif (curr_i <= 1) Messag...
the string is basically plain c code with unix line endings and supposed to be treated in a way that the values of some specific variables are replaced by something else gathered from a Qt UI.
I tried the following to do the replacing:
tmplt.replace(u"<<<compute_type>>>", str(led_coeffs.compute_type))
where 'led_coeffs' is a namedtuple and its value is an integer. I also tried this:
tmplt = Template(u'\t\t\tcase ${compute_type}:\n\t\t\t\t{\n\t\t\t\t\tif (curr_i <= 1) Messag...)
tmplt.substitute(compute_type = str(led_coeffs.compute_type))
however, both approaches do not work and I have no idea why. Finally I was hoping to get some input here. Maybe the whole approach is not right and any hint on how to achieve the replacing in a good manner is highly appreciated.
Thanks,
Ben
str.replace (and other string methods) don't work in-place (string in Python are immutable) - it returns a new string - you will need to assign the result back to the original name for the changes to take effect:
tmplt = tmplt.replace(u"<<<compute_type>>>", str(led_coeffs.compute_type))
You could also invent your own kind of templating:
import re
print re.sub('<<<(.*?)>>>', lambda L, nt=led_coeffs: str(getattr(nt, L.group(1))), your_string)
to automatically lookup attributes on your namedtuple...

Resources